Page. clientscript-obtains the clientscriptmanager object used to manage scripts, register scripts, and add scripts to pages.
Clientscriptmanager class: defines a method for managing client scripts in a web application.
By adding scripts to the HTML Tag of a webpage, you can add client scripts to the webpage in declarative mode. However, in some cases, you need to dynamically add client scripts. To dynamically Add a script, use the registerclientscriptblock method, registerclientscriptinclude method, registerstartupscript method, or registeronsubmitstatement method based on the time and method you want to add the script.
I. Dynamically add client scripts to ASP. NET web pages.
You can use the server code to add client scripts to the page. It is useful to use server code to create a client script when the content of the client script is dependent on the information that is available until it is run. It is also useful to dynamically Add a client script to the page when you want the client script to be executed in the following cases:
When the page has been loaded
When a user submits a page
-
| Method |
Description |
Registerclientscriptblock |
Add a script block to the top of the page. Create a script as a string and pass it to the method. The method then adds the script to the page. You can use this method to insert any script into the page. Note that the script may be rendered to the page before all elements are completed. Therefore, you may not be able to reference all elements on the page from the script. |
Registerclientscriptinclude |
AndRegisterclientscriptblockThe method is similar, but this method adds a script block that references an external. js file. Include files added before any other dynamically added scripts; therefore, you may not be able to reference certain elements on the page. |
Registerstartupscript |
Add a script block to the page, which triggersOnloadBefore the event. This script is generally not created as an event handler or function; it usually only contains the statement to be executed once. |
Registeronsubmitstatement |
Add the response pageOnsubmitThe script executed by the event. This script is executed before the submission page. You can cancel the submission. |
The following code example shows how to add a client script to the page that is executed when the user clicks the button (this button returns the page to the server. The client script is displayed in the pop-up window, requesting the user to confirm sending back.
Protected void <br/> page_load (Object sender, eventargs e) <br/>{< br/> string scripttext = "Return confirm ('Do you want to submit the page? ') "; <Br/> clientscript. registeronsubmitstatement (this. GetType ()," confirmsubmit ", scripttext); <br/>}
II.Add client script events to ASP. NET web server controls: Add client scripts to controls on ASP. NET web pages in a declarative manner, just like HTML elements. Alternatively, if the event or code dependency is only available at runtime, you can add client script events to the ASP. NET web server control programmatically.
There are two methods:
1. Add a client event handler to the ASP. NET Server Control in declarative mode.<% @ Page Language = "C #" %> <br/> <HTML> <br/> <pead runat = "server"> <br/> <title> untitled page </title> <br/> <MCE: script Type = "text/JavaScript"> <! -- <Br/> var previuscolor; <br/> function makered () <br/> {<br/> previuscolor = Window. event. srcelement. style. color; <br/> window. event. srcelement. style. color = "# ff0000"; <br/>}< br/> function restorecolor () <br/>{< br/> window. event. srcelement. style. color = previuscolor; <br/>}</P> <p> // --> </MCE: SCRIPT> <br/> </pead> <br/> <body> <br/> <Form ID = "form1" runat = "server"> <br/> <asp: button id = "button1" runat = "server" <br/> text = "button1" <br/> onmouseover = "makered ();" onmouseout = "restorecolor (); "/> <br/> </form> <br/> </body> <br/> </ptml>
2. Add a client event handler to the ASP. NET control programmatically
Protected void page_load (Object sender, eventargs e) {string scripttext = "Return confirm ('Do you want to submit the page? ') "; Clientscript. registeronsubmitstatement (this. GetType ()," confirmsubmit ", scripttext );}3. Add the client onclick event to the button control
<Asp: button id = "button1" runat = "server" onclick = "button#click" onclientclick = "Return confirm ('ready to submit. ') "text =" test client click "/> ClientscriptmanagerClass uniquely identifies the script by the string and type keys. Scripts with the same key and type are considered as repeated scripts. Using script types helps avoid obfuscation of similar scripts that may be used in pages from different user controls.
You can useClientscriptmanagerClass to call the client callback. This is called an out-of-band callback to the server. In the client callback, the client script function sends an asynchronous request to the ASP. NET webpage. Modify the normal lifecycle of a webpage to process callback. Use the getcallbackeventreference method to obtain a reference to the client function. When this function is called, it starts a client callback for server-side events.
The clientscriptmanager. getcallbackeventreference method is used to obtain a reference to the client function. When this function is called, a client callback for server events is started.
Public String getcallbackeventreference (Control, // server control that processes client callback. This control must implement the icallbackeventhandler interface and provide the raisecallbackevent method. String argument, // a string clientcallback parameter passed from the client script to the server, // The name of a client event handler. the handler receives the result of a successful server event. String context, // The client script calculated on the client before the callback is started. The result of the script is returned to the client event handler .)
<% @ Page Language = "C #" %> <% @ implements interface = "system. Web. UI. icallbackeventhandler" %> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <MCE: script runat = "server"> <! -- Public int cbcount = 0; // define method that processes the callbacks on server. public void raisecallbackevent (string eventargument) {cbcount = convert. toint32 (eventargument) + 1;} // define method that returns callback result. public String getcallbackresult () {return cbcount. tostring ();} protected void page_load (Object sender, eventargs e) {// define a stringbuilder to hold messages to o Utput. stringbuilder sb = new stringbuilder (); // check if this is a PostBack. SB. append ("no page postbacks have occurred. "); If (page. ispostback) {sb. append ("a page PostBack has occurred. ");} // write out any messages. mylabel. TEXT = sb. tostring (); // get a clientscriptmanager reference from the page class. clientscriptmanager cs = page. clientscript; // define one of the callback script's Conte XT. // The callback script will be defined in a script block on the page. stringbuilder context1 = new stringbuilder (); context1.append ("function javaseserverdata1 (ARG, context)"); context1.append ("{"); context1.append ("message1.innertext = ARG ;"); context1.append ("value1 = ARG;"); context1.append ("}"); // define callback references. string cbreference1 = cs. getcallbackeventreference (this, "Arg ", "Eseserverdata1", context1.tostring (); string cbreference2 = cs. getcallbackeventreference ("'" + Page. uniqueid + "'", "Arg", "receiveserverdata2", "", "processcallbackerror", false); string callbackscript1 = "function calltheserver1 (ARG, context) {"+ cbreference1 +" ;}"; string callbackscript2 = "function calltheserver2 (ARG, context) {" + cbreference2 + ";}"; // register script blocks will Perform call to the server. CS. registerclientscriptblock (this. getType (), "calltheserver1", callbackscript1, true); CS. registerclientscriptblock (this. getType (), "calltheserver2", callbackscript2, true);} // --> </MCE: SCRIPT> <MCE: Script Type = "text/JavaScript"> <! -- Var value1 = 0; var value2 = 0; function extends eserverdata2 (ARG, context) {message2.innertext = ARG; value2 = ARG;} function processcallbackerror (ARG, context) {message2.innertext = 'an error has occurred. ';} // --> </MCE: SCRIPT> <HTML> <pead id = "head1" runat = "server"> <title> clientscriptmanager example </title> </pead> <body> <Form ID = "form1 "runat =" server "> <div> callback 1 result: <span id = "message1"> 0 </span> <br/> callback 2 result: <span id = "message2"> 0 </span> <br/> <input type = "button" value = "clientcallback1" onclick = "calltheserver1 (value1, alert ('increment value') "/> <input type =" button "value =" clientcallback2 "onclick =" calltheserver2 (value2, alert ('increment value ')) "/> <br/> <asp: Label id =" mylabel "runat =" server "> </ASP: label> </div> </form> </body> </ptml>(Most of the above are from msdn)
More topics:
Client callback example with verification implementation Generate an ASP. NET web application
Client callback example with verification implementation Generate ASP. NET web applications in Visual Studio
Implement client callback programmatically on ASP. NET web pages without sending back Generate ASP. NET web applications in Visual Studio
Implement client callback without sending back in ASP. NET webpage Generate an ASP. NET web application
How to: implement callback in ASP. NET web pages Generate an ASP. NET web application
Client callback implementation (C #) Example Generate an ASP. NET web application