Usage of alert, confirm, and prompt in ASP. NET

Source: Internet
Author: User

In Asp.net, you may want to pop up some information boxes when performing an operation: the warning message box popped up by alert, and the confirmation and cancellation buttons pop-up by confirm, and the input data dialog box popped up by prompt.
There are two methods to achieve this.
One is implemented using JavaScript functions. The Code will be used in the source code (HTML language) of the. aspx file, and JavaScript functions will be called in the HTML control to implement alert, confirm, and prompt.
The other is used in the. aspx. CS file of the web form. Alert and prompt can be directly used using response. Write (). Confirm needs to add it to the properties of the Web server control. Here are several examples for these two methods.

1. We create two HTML buttons to call JavaScript Functions respectively. One button is used to pop up the confirm information box, and the other is used to pop up the prompt information box. The operation result is displayed using alert.

<HTML>
<Head>
<Title> sugon blog </title>
<Script language = "ecmascript">
<! --
Function Delcheck ()
{
VaR flag = Window. Confirm ("are you sure you want to delete it? ");
If (flag = true)
Alert ("deleted! ");
}
Function datacheck ()
{
VaR DATA = Window. Prompt ("enter a number between 1 and 10", "7 ");
If (Data! = NULL)
Alert ("You entered:" + data );
}
// -->
</SCRIPT>
</Head>
<Body>
<Form ID = "form1">
<Input name = "Del" type = "button" id = "button2" value = "delete record" onclick = "Delcheck ()">
<Input name = "in" type = "button" id = "in" value = "input data" onclick = "datacheck ()">
</Form>
</Body>
</Html>

2. Use the following in the. aspx. CS file of ASP. NET:
(1) Use of alert:

Response. Write ("<SCRIPT> alert ('the password is incorrect! ') </SCRIPT> ");

(2) Use of confirm: click the button button3 or not. The confirm dialog box is displayed. When "OK" is selected, the corresponding button operation is started.
Add the following code in page_load:

Protected void page_load (Object sender, eventargs E)
{
Button3.attributes. Add ("onclick", "Return confirm ('Are you sure you want to modify it? ');");
}
Protected void button3_click (Object sender, eventargs E)
{
// Button operation for button3, which can be customized
Response. Write ("You selected the confirm operation! ");
}

(3) Use of prompt:

Response. Write ("<SCRIPT> var result = Window. Prompt ('enter a new file name :) ', 'abc'); If (result! = NULL) Alert ('your input is '+ result); </SCRIPT> ");

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.