ASP. NET1.1, ASP. NET2.0, ASP. NET3.5, etc.

Source: Internet
Author: User

On the asp.net page, when a verification control exists and you want to verify the control, a confirmation dialog box is displayed, prompting whether to continue.

When the onclick = "return confirm ('Are you sure to continue? ') ", The verification control will be invalid. Because the verification control is also an onclick event for adding the client.

You can use the following methods in asp. net2.0 and asp. net3.5. Asp. net1.1)

First, set the CausesValidation of the button to "false", that is

 
 
  1. < asp:Button ID="Button1" runat="server" Text="Button"  CausesValidation="False"  OnClick="clickme" /> 

In fact, CausesValidation = "False/True" will not have any impact.

Then, register the onclick event of the client in the Page_Load event of the background code.

Method 1:

 
 
  1. protected void Page_Load(object sender, EventArgs e)  
  2.     {  
  3.         Button1.OnClientClick = ClientScript.GetPostBackEventReference(  
  4.             new PostBackOptions(Button1, "", "", false, true, false, false, true, ""))   
  5.             + ";return (Page_IsValid && confirm('Are you sure to continue?'));";   
  6.     }  

Note that you cannot place it in if (! IsPostBack) {...}, otherwise, the first normal, the next start will report

Microsoft JScript runtime error: 'webform _ postbackoptions' undefined

That is, the onclick event must be re-registered every time it is triggered.

This problem occurred during the test yesterday. It may be due to a problem with VS2008 installation (the design mode cannot be displayed ),

I tested it today and can register it only once, that is, when the page is loaded, as follows:

 
 
  1. protected void Page_Load(object sender, EventArgs e)  
  2.     {  
  3.         if (!IsPostBack)  
  4.         {  
  5.             Button1.OnClientClick = ClientScript.GetPostBackEventReference(  
  6.                 new PostBackOptions(Button1, "", "", false, true, false, false, true, ""))  
  7.                 + ";return (Page_IsValid && confirm('Are you sure to continue?'));";  
  8.         }  
  9.           
  10.     }  

Verification Control Method 2:

 
 
  1. protected void Page_Load(object sender, EventArgs e)  
  2.     {  
  3.         if (!IsPostBack)  
  4.         {  
  5.             Button1.OnClientClick = "javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('Button1', '', true, '', '', false, false));return (Page_IsValid && confirm('Are you sure to continue?'));WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('Button1', '', true, '', '', false, false))";  
  6.         }  
  7.     }  

In fact, after method 1 is run, The onclick code generated in the client html is the code in method 2.

If you use method 2 to directly write javascript strings in the background, you can remove the last sentence. otherwise, an additional verification is performed. That is:

 
 
  1. protected void Page_Load(object sender, EventArgs e)  
  2.     {  
  3.         if (!IsPostBack)  
  4.         {  
  5.             Button1.OnClientClick = "javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('Button1', '', true, '', '', false, false));return (Page_IsValid && confirm('Are you sure to continue?'));";  
  6.         }  
  7.     }  

Method 1 requires registration in page_load before each button is clicked, and method 2 requires registration in page_load.

Vb.net is slightly different from the html client code generated by C #.

 
 
  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
  2.         If Not Me.Page.IsPostBack Then  
  3.             Me.Button1.OnClientClick = "javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('Button1', '', true, 'name', '', false, false));return (Page_IsValid && confirm('Are you sure to continue?'));" 
  4.         End If  
  5.     End Sub  

Or

 
 
  1. Protected Sub Page_PreRender (ByVal sender As Object, ByVal e As System. EventArgs) Handles Me. PreRender
  2. If Not Me. Page. IsPostBack Then
  3. In 'vb. NET, place the following sentence in Page_PreRender. It cannot be placed in Page_Load, and you only need to register it once.
  4. 'Some of the client code that generates html is less than C.
  5. Me. Button1.OnClientClick = Me. Page. ClientScript. GetPostBackEventReference (New System. Web. UI. PostBackOptions (Me. Button1,"","", False, True, False, False, True,"Name"))&_
  6. "; Return (Page_IsValid & confirm ('Are you sure to continue? '));" 
  7. End If
  8. End Sub

The introduction of asp. net2.0 and asp. net3.5 is complete. The solution in asp.net 1.1 is introduced below.

First, set the CausesValidation of the button to "false", that is

 
 
  1. < asp:Button ID="Button1" runat="server" Text="Button"  CausesValidation="False"  OnClick="clickme" /> 

Then, register the onclick event of the client in the Page_Load event of the background code.

 
 
  1. Private VoidPage_Load (ObjectSender, System. EventArgs e)
  2. {
  3. // Place user code here to initialize the page 
  4. If(!This. Page. IsPostBack)
  5. {
  6. StringMsg ="Javascript: if (typeof (Page_ClientValidate) = 'function') {if (Page_ClientValidate () return window. confirm ('Are you sure to continue? ');}";
  7. This. Button1.Attributes. Add ("Onclick", Msg );
  8. }
  9. }

Because the onclick code registered in asp. net1.1 only contains javascript code, it can be placed in if (! IsPostBack.

In this way, the control is verified and the confirmation dialog box is displayed.

  1. Implementation of ASP. net mvc paging controls
  2. ASP. net mvc instance: using the Northwind and Entity frameworks
  3. How ASP. NET works
  4. Overview of ASP. NET cookie operations
  5. ASP. NET obtains the code of the primary key of the inserted row.

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.