简体   繁体   English

当网页在C#中从一页导航到另一页时如何执行代码

[英]How to execute code when webpage navigate from one page to another in C#

private void button1_Click(object sender, EventArgs e)
{
  HtmlDocument doc = webBrowser1.Document;
  HtmlElement from = doc.GetElementById("fromStation");
  HtmlElement to = doc.GetElementById("toStation");
  HtmlElement d = doc.GetElementById("journeyDateInputDate");

  HtmlElement s = doc.GetElementById("ticketType");
  HtmlElement ticket = doc.GetElementById("ticketType");
  HtmlElement submit = doc.GetElementById("jpsubmit");
  HtmlElement hcab = doc.GetElementById("handicapPassengers");

  from.SetAttribute("value", textBox3.Text);
  to.SetAttribute("value", textBox4.Text);
  d.SetAttribute("value", textBox5.Text);
  ticket.SetAttribute("value", Properties.Settings.Default["ticket"].ToString());
  string com = "true";

  if (Properties.Settings.Default["check"].ToString() == com)
    hcab.InvokeMember("click");
  submit.InvokeMember("click");
}

I am making project on c# where i have to execute code when webpage navigate from one page to another and when web load completely. 我在c#上制作项目,当网页从一个页面导航到另一页面并且完全加载网页时,我必须执行代码。 I have used button to execute a code when webpage completely loads....but now i what it to execute without using button 我已使用按钮在网页完全加载时执行代码。...但是现在我不使用按钮即可执行的代码

To provide a substantial and correct answer, you might want to provide more specifics on the environment. 要提供实质性和正确的答案,您可能需要提供有关环境的更多详细信息。 But assuming a whole bunch of details, I'm making an (un)educated guess here. 但是假设有很多细节,我在这里做一个(未受过教育的)猜测。

If it's MVC project, you can execute the code as you're presenting the next view. 如果是MVC项目,则可以在呈现下一个视图时执行代码。 If the page is navigated to from JS (which is on the client) or simply navigated away from your site, it might be much more tricky. 如果页面是从JS(位于客户端)上导航到的,或者只是从您的站点导航而来,则可能会更加棘手。

In any case, since it's an operation on the client, you'll need to manage that from JS on the client . 无论如何,由于它是在客户端上执行的操作,因此您需要通过client上的 JS管理该操作。 The server has let the contents go and the page is viewed in the browser even of the server goes down. 服务器放走了内容,即使服务器掉线了,也可以在浏览器中查看页面。

$(function(){
  alert("Page loaded.");
  // do other stuff
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c# QueryString 如何将数据从一个网页发送到另一个网页? - c# QueryString how to send data from one webpage to another? 如何在 React JS 中从一个页面导航到另一个页面? - how to navigate from one page to another in react js? 单击按钮从一个页面导航到另一个页面 - Navigate from one page to another on Button click 使用C#代码通过Javascript从一页传递数据并在另一页上捕捉数据 - Pass data through Javascript from one page and catch data on another page using c# code 如何将页面导航到另一视图? - how to navigate one page to another view? 如何执行在另一页中写在一页中的JavaScript? - How to execute JavaScript writen in one page from another page? 使用angular2单击“提交”按钮时,从一页导航到另一页 - Navigate from one page to another page when submit button is clicked using angular2 如何从JavaScript代码执行C#方法而不刷新页面? - How to execute a C# method from JavaScript code without refreshing the page? 导航到另一个页面时如何取消setTimeout? - How to cancel setTimeout when navigate to another page? 如何在我的phonegap应用程序中从网页导航到本地页面? - How to navigate from a webpage to local page in my phonegap app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM