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.
Code
<% @ 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
Registerstartupscript is slightly different from registerclientscriptblock. 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)
So what is the difference between the script registration process on the page? In fact, there is a big difference!
If you have some JavaScript code for processing controls on the page, you should use the registerstartupscript method in most cases, instead of the registerclientscriptblock method.
That is, page. clientscript. registerstartupscript is used when page controls are searched and called.
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. 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 );
Syntax C #
Public void registerclientscriptblock (type, string key, string script, bool addscripttags)
Parameters
-
The type of the client script to be registered.
-
Key of the client script to be registered.
-
The client script Text to be registered.
-
Addscripttags indicates whether to add a Boolean value of the script tag.
Public void registerstartupscript (type, string key, string script, bool addscripttags)
Parameters
-
Type indicates the type of the startup script to be registered.
-
Key of the startup script to be registered.
-
The STARTUP script text to register.
-
Addscripttags indicates whether to add a Boolean value of the script flag.
Public void registerstartupscript (type, string key, string script)
Parameters
-
Type indicates the type of the startup script to be registered.
-
Key of the startup script to be registered.
-
The STARTUP script text to register.