The use of confirm in asp.net can be divided into two categories:
1. No use of ajax,confirm will cause also face refresh
2. Using AJAX, does not refresh
A. Not using AJAX, you can use StringBuilder to complete.
(a) ASP.net with StringBuilder control background operation Javascript:confirm return value, this method is relatively cumbersome
1. Background Startup events
1 2 3 4 5 6 7 |
StringBuilder sb = new StringBuilder (); Sb. Append ("<script language= ' JavaScript ' >"); Sb. Append ("var val=window.confirm (' Are you sure! ');"); Sb. Append ("Window.document.getElementById (' TextBox1 '). Value=val;"); Sb. Append ("__doPostBack" (' TextBox1 ', '); "); Sb. Append ("</script>"); This. RegisterStartupScript (System.Guid.NewGuid (). ToString (), SB. ToString ()); |
2. Front Code:
The
code is as follows:
<asp:textbox id= "TextBox1" runat= "Server" autopostback= "true" ontextchanged= "textbox1_textchanged" ></ Asp:textbox>
3. Text box ontextchanged= "textbox1_textchanged" background event code
1 2 3 4 5 6 7 8 9 10 11 12 13 14-15 16 |
protected void textBox1_TextChanged (object sender, EventArgs e) {if (((TextBox) (sender)). Text!= "") {if (((TextBox) (sender)). Text.toupper () = = "TRUE" {//OK to execute another Event/method} if (((TextBox) (sender)). Text.toupper () = = "FALSE" {//Cancel execution of another event/method}}} |
The event can also be written in other ways to drive. Just judge the value of TexBox1.
Note: This event causes page refreshes by autopostback= "true" and global variables are lost, preferably using the session variable to hold the value that needs to be stored.
(b) through the foreground JavaScript to determine the background of the implementation of the method, there will be more than a OnClientClick event this method is the simplest
Foreground button Events
The
code is as follows:
<asp:button id= "Bt_deletebutton" runat= "Server" onclick= "Bt_deletebutton_click" if (onclientclick=) (' Call the background Bt_deletebutton_click event, OK? ')) {return true;} Else{return false;} " text= "Delete" visible= "False"/>
Just add on the page
A.onclick Background events,
B.onclientclick event, JavaScript confirm, select OK after execution onclick background is event Bt_deletebutton_click
B: Pages that use AJAX controls
The
code is as follows:
Scriptmanager.registerstartupscript (UpdatePanel1, Updatepanel1.gettype (), "", "alert (' hint '); if (Confirm (' Whether to continue Yes) {alert (' This page continues to add ')}else{window.open (' productmanage.aspx ', ' _blank ');} ", true);