Asp.net operation javascript: Two Methods of returning confirm values
In asp.net, confirm can be divided into two types:
1. If ajax is not used, confirm will also cause refreshing.
2. If ajax is used, it will not be refreshed.
A. If ajax is not used, you can use StringBuilder.
(1) asp.net uses StringBuilder to control the return value of javascript: confirm in background operations. This method is 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 ("your own Doc ument. getElementById ('textbox1'). value = val ;"); Sb. Append ("_ doPostBack ('textbox1 ','');"); Sb. Append ("</script> "); This. RegisterStartupScript (System. Guid. NewGuid (). ToString (), sb. ToString ()); |
2. Front-end code:
The Code is as follows:
<Asp: TextBox ID = "TextBox1" runat = "server" AutoPostBack = "true" OnTextChanged = "textbox#textchanged"> </asp: TextBox>
3. Background Event code of the text box OnTextChanged = "TextBox1_TextChanged"
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 ") { // Execute another event/method after confirmation } If (TextBox) (sender). Text. ToUpper () = "FALSE ") { // Execute another event/method after cancellation } } } |
This event can also be written as another method driver. Only determine the value of TexBox1.
Note: This event is caused by AutoPostBack = "true" to refresh the page, and global variables are lost. It is best to use session variables to save the values to be stored.
(2) determine the method to be executed in the background by using the front-end Javascript, and an OnClientClick event is added, which is the simplest method.
Foreground button event
The Code is as follows:
<Asp: Button ID = "bt_DeleteButton" runat = "server" OnClick = "bt_DeleteButton_Click" OnClientClick = "if (confirm ('Call the background bt_DeleteButton_Click event. Are you sure you want to continue? ') {Return true;} else {return false;} "Text =" delete "Visible =" False "/>
Add
A. OnClick background event,
B. OnClientClick event, confirm of javascript. After you select OK, run the OnClick event bt_DeleteButton_Click in the background.
B: pages with Ajax controls
The Code is as follows:
ScriptManager. registerStartupScript (UpdatePanel1, UpdatePanel1.GetType (), "", "alert ('hprompt '); if (confirm ('whether to continue executing yes )) {alert ('add more on this page ')} else {window. open ('productmanage. aspx ',' _ blank ');} ", true );