Summary of methods in the ASP. NET pop-up message dialog box

Source: Internet
Author: User

Principle]

Place a hidden control on the page and put a script code at the end of the page. The script code checks whether the value of the hidden control is null. If not empty, a dialog box is displayed, otherwise, do nothing.
When the background code needs to modify the value of the hidden control, when the page is uploaded to the user, the final script code is executed and a dialog box is displayed.
 
[Note]
1. The hidden control must be an HTML control, otherwise JavaScript cannot be found.
2. You need to modify the value of the hidden control in the background code. The hidden control must be marked with runat = "server.
3. After the dialog box is displayed, remember to leave the value of the hidden control empty. Otherwise, the value will pop up again during refresh.
4. The script code must be placed behind the hidden control; otherwise, it cannot be found.
 
[Implementation]
Page code (only list the body)
<Body ms_positioning = "gridlayout">
<Form ID = "form1" method = "Post" runat = "server">
<Asp: textbox id = "manuinput" runat = "server"> </ASP: textbox>
<Asp: button id = "button1" runat = "server" text = "dialog box"> </ASP: button>
<Input id = "passtxt" type = "hidden" runat = "server"> <! -Hide controls.
</Form>
<Script language = JavaScript>...
If (document. All ("passtxt"). value! = "")
...{
Alert (document. All ("passtxt"). value );
Document. All ("passtxt"). value = ""; // This sentence cannot be dropped!
}
</SCRIPT>
</Body>
Background code (only list the response events of button1)
Private void button#click (Object sender, system. eventargs E)
{
Passtxt. value = manuinput. text;
}
[Effect]
 
[Additional description]
In fact, this method is very simple, but it is very effective. You can write a function showdialog (string Str) and call it wherever necessary in the background Code to bring up a dialog box.
In addition, you can replace alert with showmodeldialog () to bring up a window with richer functions and more beautiful interfaces.
Finally, it must be noted that the idea of this method can be used to transmit information between the script and the background code. I did this in my web application, and the effect is very good.
During the development of ASP. NET programs, we often need to provide users with prompts, such as whether the operation is successful, whether the operation is OK or canceled.

(1) click the button on the page and a dialog box is displayed, prompting whether to "OK" or "cancel". We can add attributes to the button to complete the operation:
Example:
Public System. Web. UI. webcontrols. Button btndelrow;
Btndelrow. Attributes. Add ("onclick", "Return confirm ('Are you sure you want to delete it? ');");
(2) click the link on the page. A dialog box is displayed, indicating whether to "OK" or "cancel" the operation. You can click the link in the page_load () event, add a property to the button that you want to display a confirmation prompt:
Example:
Link. Attributes. Add ("onclick", "Return confirm ('do you want to perform this operation? ');");

(3) After completing an operation on the page, a dialog box is displayed, prompting whether the operation is successful ".
Example:
Response. Write ("<SCRIPT>... alert ('deleted successfully! ') </SCRIPT> ");

(4) Allow the ASP. NET Server Control to issue client script blocks in the page:
Public Virtual void registerstartupscript (string key, string script );
Example:
If (! This. isstartupscriptregistered ("hello "))
This. registerstartupscript ("hello", "<SCRIPT>... alert ('Hello! ') </SCRIPT> ");

 

The "OK" dialog box is displayed:
Response. Write ("<script language = JavaScript>... alert ('message! '); </SCRIPT> ");

The "OK" dialog box is displayed. Click to jump to the page:
Response. Write ("<script language = JavaScript>... alert ('message! '); Window. navigate (' ../index. aspx '); </SCRIPT> ");

The "OK" and "cancel" dialog boxes are displayed. Click "OK" to perform the following operations:
Method 1: In the page_load event, write button1.attributes ["onclick"] = "javascript: Return confirm ('Are you sure you want to delete it? '); ", And then write your Execution Code in the button1_onclick event.
Method 2: write data directly in the button1_onclik event
Response. Write ("<script language = JavaScript>... If (confirm ('Are you sure you want to delete it? '))... {Window. navigate ('dodelete. aspx ') ;}</SCRIPT> "); method 2 needs to jump to another page to perform the operation, which is a little more complicated than method 1, but how to use it depends on the specific situation.

Collected. Appliances
Using system;
/// <Summary>
/// Summary of alert.
/// </Summary>
Public class alert
{
Public static void showalert (string message)
{
If (Message = NULL)
Message = "";
Message = message. Replace ("",""");
System. Web. httpcontext. Current. response. Write ("<SCRIPT>... alert ('" + message + "'); </SCRIPT> ");
}
Public static void showalert (string message, string URL)
{
If (Message = NULL)
Message = "";
Message = message. Replace ("",""");
System. web. httpcontext. current. response. write ("<SCRIPT>... alert ("" + message + ""); location = '"+ URL +"'; </SCRIPT> ");
}
Public static void showconfirmalert (string message, string confirmurl, string cancelurl)
...{
If (Message = NULL)
Message = "";
Message = message. Replace ("",""");
System. web. httpcontext. current. response. write ("<script language = JavaScript> If (confirm ('" + message + "') {document. location. href = '"+ confirmurl +"';} else {document. location. href = '"+ cancelurl +"'} </SCRIPT> ");
}
Public static void showconfirmalert (string message, string confirmurl)
...{
If (Message = NULL)
Message = "";
Message = message. Replace ("",""");
System. web. httpcontext. current. response. write ("<script language = JavaScript> If (confirm ('" + message + "') {document. location. href = '"+ confirmurl +"';} else {window. history. back () ;}</SCRIPT> ");
}
Public static void redirect (string URL)
...{//
//
If (url = NULL | URL. Length <1)
Showalert ("the redirection address cannot be blank ");
Else
System. Web. httpcontext. Current. response. Write ("<SCRIPT> location = '" + URL + "'; </SCRIPT> ");
}
Public static void ssologinredirect (string URL)
...{
Redirect (URL );
// If (url = NULL | URL. Length <1)
// Showalert ("the redirection address cannot be blank ");
// Else
// System. Web. httpcontext. Current. response. Write ("<SCRIPT> If (window. parent! = Window) window. Parent. Location = Window. Location; location = '"+ URL +"'; </SCRIPT> ");
 
}
Public static void showalert (string message, string URL, bool isredirect)
...{

If (Message = NULL)
Message = "";
If (isredirect)
Showalert (message, URL );
Else
Showalert (Message );
}
}

Reprinted statement:This article from csdn blog http://blog.csdn.net/linxi27/archive/2008/01/31/2075028.aspx

========================================================== ======================================

 

Asp.net background message dialog box! [Switch]

 

In the winform background, we use MessageBox. show ("message") to return background information. In the webform background, we use response. write ("message") to return the background message, but the message value needs to be displayed as a string to the foreground page, whether there is similar to MessageBox. show ("message") method to pop up information. We will soon think of the Javascript alert method. Alert is called by calling Js in the background. Usage: response. Write ("<SCRIPT> alert ('" + value + "') </SCRIPT> ");
Of course, in the development process of ASP. NET programs, we often need to give users a prompt, for example, whether to "operate successfully", "OK" or "cancel" the operation.

(1) click the button on the page and a dialog box is displayed, prompting whether to "OK" or "cancel". We can add attributes to the button to complete the operation:
Example:
Public System. Web. UI. webcontrols. Button btndelrow;
Btndelrow. Attributes. Add ("onclick", "Return confirm ('Are you sure you want to delete it? ');");
(2) click the link on the page. A dialog box is displayed, indicating whether to "OK" or "cancel" the operation. You can click the link in the page_load () event, add a property to the button that you want to display a confirmation prompt:
Example:
Link. Attributes. Add ("onclick", "Return confirm ('do you want to perform this operation? ');");

(3) After completing an operation on the page, a dialog box is displayed, prompting whether the operation is successful ".
Example:
Response. Write ("<SCRIPT>... alert ('deleted successfully! ') </SCRIPT> ");

(4) Allow the ASP. NET Server Control to issue client script blocks in the page:
Public Virtual void registerstartupscript (string key, string script );
Example:
If (! This. isstartupscriptregistered ("hello "))
This. registerstartupscript ("hello", "<SCRIPT>... alert ('Hello! ') </SCRIPT> ");

 

The "OK" dialog box is displayed:
Response. Write ("<script language = JavaScript>... alert ('message! '); </SCRIPT> ");

The "OK" dialog box is displayed. Click to jump to the page:
Response. Write ("<script language = JavaScript>... alert ('message! '); Window. navigate (' ../index. aspx '); </SCRIPT> ");

The "OK" and "cancel" dialog boxes are displayed. Click "OK" to perform the following operations:
Method 1: In the page_load event, write button1.attributes ["onclick"] = "javascript: Return confirm ('Are you sure you want to delete it? '); ", And then write your Execution Code in the button1_onclick event.
Method 2: write data directly in the button1_onclik event
Response. Write ("<script language = JavaScript>... If (confirm ('Are you sure you want to delete it? '))... {Window. navigate ('dodelete. aspx ') ;}</SCRIPT> "); method 2 needs to jump to another page to perform the operation, which is a little more complicated than method 1, but how to use it depends on the specific situation.

Collected. Appliances

Using system;

/// <Summary>
/// Summary of alert.
/// </Summary>
Public class alert
{
Public static void showalert (string message)
{
If (Message = NULL)
Message = "";
// Ljj
// 2005-12-9
Message = message. Replace ("",""");
System. Web. httpcontext. Current. response. Write ("<SCRIPT>... alert ('" + message + "'); </SCRIPT> ");
}
Public static void showalert (string message, string URL)
{
If (Message = NULL)
Message = "";
Message = message. Replace ("",""");
System. web. httpcontext. current. response. write ("<SCRIPT>... alert ("" + message + ""); location = '"+ URL +"'; </SCRIPT> ");
}
Public static void showconfirmalert (string message, string confirmurl, string cancelurl)
...{
If (Message = NULL)
Message = "";
Message = message. Replace ("",""");
System. web. httpcontext. current. response. write ("<script language = JavaScript> If (confirm ('" + message + "') {document. location. href = '"+ confirmurl +"';} else {document. location. href = '"+ cancelurl +"'} </SCRIPT> ");
}
Public static void showconfirmalert (string message, string confirmurl)
...{
If (Message = NULL)
Message = "";
Message = message. Replace ("",""");
System. web. httpcontext. current. response. write ("<script language = JavaScript> If (confirm ('" + message + "') {document. location. href = '"+ confirmurl +"';} else {window. history. back () ;}</SCRIPT> ");
}
Public static void redirect (string URL)
...{//
//
If (url = NULL | URL. Length <1)
Showalert ("the redirection address cannot be blank ");
Else
System. Web. httpcontext. Current. response. Write ("<SCRIPT> location = '" + URL + "'; </SCRIPT> ");
}
Public static void ssologinredirect (string URL)
...{
Redirect (URL );
// If (url = NULL | URL. Length <1)
// Showalert ("the redirection address cannot be blank ");
// Else
// System. Web. httpcontext. Current. response. Write ("<SCRIPT> If (window. parent! = Window) window. Parent. Location = Window. Location; location = '"+ URL +"'; </SCRIPT> ");
 
}
Public static void showalert (string message, string URL, bool isredirect)
...{

If (Message = NULL)
Message = "";
If (isredirect)
Showalert (message, URL );
Else
Showalert (Message );
}
}

 

Reprinted statement:This article from csdn blog http://www.cnblogs.com/xionglee/articles/1540737.html

========================================================== ======================================

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.