Conflict between. Net verification controls and JavaScript

Source: Internet
Author: User

Javascript events and. Net verification controls are occasionally used when you are doing things. The details are as follows:
<Head runat = "server">
<Title> untitled page </title>
<Script language = "JavaScript" type = "text/JavaScript">
VaR isdetailsmodifed = true
Function submitdetailmodified ()
...{
VaR retvalue;
If (isdetailsmodifed = true)
...{
Retvalue = confirm ("do you want to save ?);
}
Else
...{
Alert ("No need to save because of no field modified .");
Retvalue = false
}
Return retvalue;

}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: textbox id = "txttest" runat = "server" width = "80px"> </ASP: textbox>
<Asp: regularexpressionvalidator id = "regularexpressionvalidator4" runat = "server" controltovalidate = "txttest"
Errormessage = "test must be numeric" validationexpression = "^ [0-9] {1, 28 }.? [0-9] {0, 4 }$ "> * </ASP: regularexpressionvalidator>
<Asp: button id = "button1" runat = "server" text = "button" onclientclick = "Return submitdetailmodified ()"/>
<Asp: validationsummary id = "validationsummary1" runat = "server" showmessagebox = "true" showsummary = "false"/>
</Div>
</Form>
</Body>
The above intention is to pop up a confirmation box when you click the button. If you select Yes, the return will continue; otherwise, the return will be blocked. If the verification of a field fails during the return process, validationsummary should pop up a message box to prevent the return.
However, this is counterproductive. When I click the button, If I select 'no', the backend is blocked.
But when I select "yes", there are two situations:
1. All fields are verified and click "yes". The page continues to return. Everything is normal.
2. When the verification of a field fails, click "yes". The verification message does not pop up and the page will continue to return the message, resulting in incorrect operation data.
Why can't the verification message box pop up?
Later, I learned that the mechanism of the. NET verification control is to use JavaScript to return true or false to control whether the page is returned. Above Code If yes is selected, true is returned directly in the button clicking event. Of course, the page will be returned. Later it was found that the verification control was blocked by event. returnvalue = true/false. So I changed my code as follows:
<Head runat = "server">
<Title> untitled page </title>
<Script language = "JavaScript" type = "text/JavaScript">
VaR isdetailsmodifed = true
Function submitdetailmodified ()
...{
VaR retvalue;
If (isdetailsmodifed = true)
...{
Retvalue = confirm ("do you want to save? ");
}
Else
...{
Alert ("No need to save because of no field modified .");
Retvalue = false
}
If (retvalue = false)
...{
Event. returnvalue = retvalue;
}

}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: textbox id = "txttest" runat = "server" width = "80px"> </ASP: textbox>
<Asp: regularexpressionvalidator id = "regularexpressionvalidator4" runat = "server" controltovalidate = "txttest"
Errormessage = "test must be numeric" validationexpression = "^ [0-9] {1, 28 }.? [0-9] {0, 4 }$ "> * </ASP: regularexpressionvalidator>
<Asp: button id = "button1" runat = "server" text = "button" onclientclick = "submitdetailmodified ()"/>
<Asp: validationsummary id = "validationsummary1" runat = "server" showmessagebox = "true" showsummary = "false"/>
</Div>
</Form>
</Body>
If I select "yes", I will not do anything, that is, the default will continue to return, but if there is a field that fails verification, the verification control passes the event. returnvalue = false to prevent the page from returning, so that the page will pop up the dialog box that fails verification. If I select 'no', I also use event. returnvalue = false to prevent page return. In this way, the initial goal is achieved.

Summary:
When using the. NET verification control, do not directly return true or false in the event, but implement page return blocking through event. returnvalue = true/false.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/kucool/archive/2008/02/27/2124024.aspx

Related Article

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.