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