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
- < 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:
- protected void Page_Load(object sender, EventArgs e)
- {
- Button1.OnClientClick = ClientScript.GetPostBackEventReference(
- new PostBackOptions(Button1, "", "", false, true, false, false, true, ""))
- + ";return (Page_IsValid && confirm('Are you sure to continue?'));";
- }
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:
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- Button1.OnClientClick = ClientScript.GetPostBackEventReference(
- new PostBackOptions(Button1, "", "", false, true, false, false, true, ""))
- + ";return (Page_IsValid && confirm('Are you sure to continue?'));";
- }
-
- }
Verification Control Method 2:
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- 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))";
- }
- }
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:
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- Button1.OnClientClick = "javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('Button1', '', true, '', '', false, false));return (Page_IsValid && confirm('Are you sure to continue?'));";
- }
- }
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 #.
- Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
- If Not Me.Page.IsPostBack Then
- Me.Button1.OnClientClick = "javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('Button1', '', true, 'name', '', false, false));return (Page_IsValid && confirm('Are you sure to continue?'));"
- End If
- End Sub
Or
- Protected Sub Page_PreRender (ByVal sender As Object, ByVal e As System. EventArgs) Handles Me. PreRender
- If Not Me. Page. IsPostBack Then
- 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.
- 'Some of the client code that generates html is less than C.
- Me. Button1.OnClientClick = Me. Page. ClientScript. GetPostBackEventReference (New System. Web. UI. PostBackOptions (Me. Button1,"","", False, True, False, False, True,"Name"))&_
- "; Return (Page_IsValid & confirm ('Are you sure to continue? '));"
- End If
- 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
- < 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.
- Private VoidPage_Load (ObjectSender, System. EventArgs e)
- {
- // Place user code here to initialize the page
- If(!This. Page. IsPostBack)
- {
- StringMsg ="Javascript: if (typeof (Page_ClientValidate) = 'function') {if (Page_ClientValidate () return window. confirm ('Are you sure to continue? ');}";
- This. Button1.Attributes. Add ("Onclick", Msg );
- }
- }
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.
- Implementation of ASP. net mvc paging controls
- ASP. net mvc instance: using the Northwind and Entity frameworks
- How ASP. NET works
- Overview of ASP. NET cookie operations
- ASP. NET obtains the code of the primary key of the inserted row.