Two ways to ASP.net operations javascript:confirm return values

Source: Internet
Author: User

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:


&lt;asp:textbox id= "TextBox1" runat= "Server" autopostback= "true" ontextchanged= "textbox1_textchanged" &gt;&lt;/ Asp:textbox&gt;


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:


&lt;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"/&gt;


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);
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.