C # work und for the onclientclick event return false of the button embedded in the web page in winform webbrowser after The onclick event is bound to webbrowser

Source: Internet
Author: User

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 ");
//...
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.