Asp.net background operation javascript: Confirm Return Value

Source: Internet
Author: User

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

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:
<Asp: textbox id = "textbox1" runat = "server" autopostback = "true" ontextchanged = "textbox#textchanged"> </ASP: textbox>
3. Background Event code of the text box ontextchanged = "textbox1_textchanged"
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

<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

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

 

We may encounter. in the net background, the confirm dialog box is displayed, and then click OK to execute a piece of code. Click Cancel to execute another piece of code. Of course, the confirm dialog box can also be displayed on the foreground, but sometimes it may involve computing, or retrieve page data, but the front-end is not easy to obtain, this action will be executed in the background at this time.

In the pop-up confirm dialog box, we can do this:

Place a literal control on the page and assign a value in the background:

This. ltlmsg. TEXT = "<script language = 'javascript '> If (confirm ('Confirm message') {document. getelementbyid ('closed close '). click () ;}</SCRIPT> ";

This shows that there is

Document. getelementbyid ('closed close'). Click ();

This is the key to controlling the process. In fact, a button is placed on the page and a click event is defined for the button, but the width and height of the button are both 0, we can't see it on the page, but its click event is what we want to do in the pop-up confirm dialog box. Of course, we can do another thing by clicking Cancel, simply adding a button.

 

 

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.