In a common ASP. NET environment, the following code is frequently used to bring up a dialog box on a web form:
<Span style = "font-family: simsun; font-size: 18px;"> <span style = "font-family: simsun; font-size: 18px;"> response. write ("<script language = JavaScript> alert ('failed to delete the question! ') </SCRIPT> "); </span>
However, the above Code cannot be used in the Ajax environment; otherwise, an error is reported. Because it does not regard the data as a statement for execution and only serves as a string, how can a prompt box pop up in the Ajax environment?
You can use the registerclientscriptblock () method of the scriptmanager class of Ajax to implement the pop-up dialog box function. This class can directly register the dialog box to the web form page.
The API for this method is as follows:
Clientscriptmanager. registerclientscriptblock Method
The following is a demo to familiarize you with the usage of this method.
We encapsulate all the dialogs to be popped up in the Ajax environment into a class to facilitate future calls, such as the dialog box popped up in the page, in the Ajax environment, the pop-up dialog box of the button is encapsulated into a class and can be called later.
<Span style = "font-family: simsun; font-size: 18px; ">/// <summary> /// display the dialog box in the Ajax environment /// </Summary> public class ajaxcommond {/// <summary> /// in the ASP. in the. NET Ajax environment, A prompt dialog box is displayed for the button control. /// </Summary> /// <Param name = "button"> button control </param> /// <Param name = "Message "> message in the dialog box </param> Public void opendialogforbutton (Button button, string message) {scriptmanager. registerclientscriptblock (button, typeof (button), datetime. now. tostring (). replace (":", ""), // use the current time as the identifier "alert ('" + message + "')", true );} /// <summary> /// in ASP. in the. NET Ajax environment, A prompt dialog box is displayed for the Page Object. /// </Summary> /// <Param name = "button"> Page Object </param> /// <Param name = "Message "> message in the dialog box </param> Public void opendialogforpage (page, string message) {scriptmanager. registerclientscriptblock (page, typeof (PAGE), // The passed type datetime. now. tostring (). replace (":", ""), // use the current time as the identifier "alert ('" + message + "')", // The information to be displayed is true); }}</span>
With this public class, the Demo code for calling is as follows:
<Span style = "font-family: simsun; font-size: 18px;"> protected void button#click (Object sender, eventargs e) {ajaxcommond AC = new ajaxcommond (); // instantiate the public class ajaxcommond // The prompt dialog box in the Ajax environment is displayed. opendialogforbutton (button) sender, "data is saved successfully! ") ;}</Span>
Summary
Of course, implementing the pop-up dialog box in the Ajax environment is more than this method. It is better to have more exchanges and learn from each other.
A prompt box is displayed in the Ajax environment.