The topic in this example is based on the following situations:
There is a button on the webpage
<Asp: button id = "button1" onclientclick = "Return fnbutton1 ();" runat = "server" text = "button" onclick = "button#click"/>
Under normal circumstances, the following code is executed normally
<Script language = "JavaScript" type = "text/JavaScript">
Function fnbutton1 ()
{
If (confirm ("continue execution? "))
{
Return true;
}
Else
{
Return false;
}
}
</SCRIPT>
That is, after the confirm box pops up, The onclick background event can be executed normally or stopped based on the user's selection.
However, when the webpage is accessed by winform through webbrowser and an event is added to the webpage's button1
Void webbrowserappsdocumentcompleted (Object sender, webbrowserdocumentcompletedeventargs E)
{
If (this. webbrowser1.document. All ["button1"]! = NULL)
This. webbrowser1.document. All ["button1"]. detacheventhandler ("onclientclick", new eventhandler (html_button#click ));
}
Void html_button#click (Object sender, eventargs E)
{
//...
}
Whether or not the above confirm is selected will continue to execute the background code and the code in winform
The following code can be used to determine whether to execute the background code and the code in winform.
Add one more JavaScript function to the webpage.
<Script language = "JavaScript" type = "text/JavaScript">
VaR issubmit = false;
Function fnbutton1 ()
{
If (confirm ("continue? "))
{
Issubmit = true;
}
Else
{
Issubmit = false;
}
Return issubmit;
}
Function fnbutton1forwebbrowser ()
{
Return issubmit;
}
</SCRIPT>
Add more judgment in winform
Void html_button#click (Object sender, eventargs E)
{
Object result = This. webbrowser1.document. invokescript ("fnbutton1forwebbrowser ");
If (result. tostring (). tolower () = "true ")
{
MessageBox. Show ("executed ");
//...
}
Else
{
MessageBox. Show ("not executed ");
//...
}
}