Registerstartupscript (Key, script)
Registerclientscriptblock (Key, script) Both methods are used to write scripts from the foreground to the background, and both accept two identical parameters. The first parameter key is the unique identifier of the inserted client script. The second parameter script is the content of the client script to be inserted into the page, including the start and end tags of <SCRIPT> </SCRIPT>. The only difference between the two methods is that the script block is registered to "where.
Registerclientscriptblock (Key, script) Send the script block at the beginning of form (immediately after the <form runat = "server"> identifier)
Use Cases:
Generally, DOM elements are not used.
Registerstartupscript(Key, script) The script block is sent at the end of the form (before the </form> identifier) and executed after the document is loaded. It is equivalent to the content in body. onload = f () {}.
Use Cases:
Average
To use DOM elements, such as modifying Dom element values
----------------------------------------------------------------------
Page. clientscript. registerclientscriptblock & page. clientscript. registerstartupscript
1. Use Page. clientscript. registerclientscriptblock
The registerclientscriptblock method can place JavaScript Functions on the top of the page.
That is, the script is used to start the page in the browser.
<% @ Page Language = " C # " %>
< Script runat = " Server " >
Protected Void Page_load ( Object Sender, eventargs E)
{
String Myscript = @" Function alerthello () {alert ('Hello ASP. net ');} " ;
Page. clientscript. registerclientscriptblock ( This . GetType (),
" Myscript " , Myscript, True );
}
</ Script >
In this example, the JavaScript function alerthello () is created as a string myscript. Then, use the page. clientscript. registerclientscriptblock method to write the script on the page. The two construction methods of the registerclientscriptblock method are as follows:
● Registerclientscriptblock (type, key, script)
● Registerclientscriptblock (type, key, script, script tag Specification)
4.4.2 use page. clientscript. registerstartupscript
the registerstartupscript method is slightly different from the registerclientscriptblock method. The biggest difference is that registerstartupscript places the script at the bottom of the ASP. NET page, rather than the top. In fact, the registerstartup script method even uses the same constructor as the registerclientscriptblock method:
● registerstartupscript (type, key, script)
● registerstartupscript (type, key, script, script tag Specification)
what is the difference between the script registration process on the page? In fact, there is a big difference!
If the page contains some JavaScript code that processes controls, the registerstartupscript method should be used in most cases, instead of the registerclientscriptblock method.
that is, the page is used to search for and call page controls. clientscript. registerstartupscript
4.4.3 use page. clientscript. registerclientscriptinclude
The last method is registerclientscriptinclude. Many developers put Javascript in. js files. This is the best way, because it is easy to apply modifications to JavaScript throughout the application.Program.You can use the registerclientscriptinclude method to register a script file on the ASP. NET page, as shown below.
String myscript = "myjavascriptcode. js"
Page. clientscript. registerclientscriptinclude ("mykey", myscript );
____________________________________________________________________________________________________________________
From msdn:
<% @ Page Language = "C #" %> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > <SCRIPT runat = "Server" > Public Void Page_load (Object sender, eventargs e) {string csname1 = "Popupscript" ; String csname2 = "Buttonclickscript" ; If (! Isclientscriptblockregistered (csname1) {string cstext1 = "<SCRIPT type = \" text/JavaScript \ ">" + "Alert ('Hello World'); </" + "Script>" ; Registerstartupscript (csname1, cstext1); // The Dom element is not used, which is in conflict with the preceding statement.
} If (! Isclientscriptblockregistered (csname2) {stringbuilder cstext2 = New Stringbuilder (); cstext2.append ( "<SCRIPT type = \" text/JavaScript \ "> function doclick (){" ); Cstext2.append ( "Form1.message. value = 'text from client script. '} </" ); Cstext2.append ( "Script>" ); Registerclientscriptblock (csname2, cstext2.tostring ()); // use Dom element }}</SCRIPT> <HTML> "Form1" Runat = "Server" > <Input type = "Text" Id = "Message" /> <Input type = "Button" Value = "Clickme" Onclick = "Doclick ()" /> </Form> </body>
1. Use Page. clientscript. registerclientscriptblock
The registerclientscriptblock method can place JavaScript Functions on the top of the page.
That is, the script is used to start the page in the browser.