From: http://cgxcn.blog.163.com/blog/static/132312422009426112839757/
Differences between response. Write and page. clientscript. registerstartupscript and page. clientscript. registerclientscriptblock
Method 1, Use response. Write, this method will put JSCodeAt the top of the page (before <HTML> ):
System. Web. httpcontext. Current. response. Write ("<script language = JavaScript> alert ('js Code'); </SCRIPT> ");
Method 2Use registerstartupscript. This method will embed the JS Code at the bottom of the page and before the form (</form>). This method is applicable to the JS Code to run after the page control is loaded:
System. Web. UI. Page page = (system. Web. UI. Page) system. Web. httpcontext. Current. Handler;
If (! Page. clientscript. isstartupscriptregistered (page. GetType (), "clientscript "))
Page. clientscript. registerstartupscript (page. GetType (), "clientscript", "<script language = JavaScript> alert ('js Code'); </SCRIPT> ");
Method 3Use registerclientscriptblock. This method embeds the JS Code at the top of the page and at the beginning of the form (<form>). This method is applicable to the js code to be executed before the control is loaded, similar to the response. write method:
System. Web. UI. Page page = (system. Web. UI. Page) system. Web. httpcontext. Current. Handler;
If (! Page. clientscript. isclientscriptblockregistered (page. GetType (), "clientscript "))
Page. clientscript. registerclientscriptblock (page. GetType (), "clientscript", "<script language = JavaScript> alert ('js Code'); </SCRIPT> ");
So what is the difference between method 2 and method 3? The main difference is that,RegisterstartupscriptThe method is to embed Javascript into the bottom of the ASP. NET page, which is exactly at the end of the element.</Form>.RegisterclientscriptblockThe method is to embed Javascript into the page to enable elements.<Form>. So what's the difference? As we will see, this is a big difference.
Here is an example. Here is how to focus on a text box on the page when the page is loaded to the browser-UseRegisterstartupscriptVisual Basic of the method:
Page. clientscript. registerstartupscript (Me. GetType (), "testing", _ "document. Forms [0] ['textbox1']. Focus ();", true)
When the browser runs at the bottom of the page and executes this small JavaScript code, a text box on the page is generated and placed on the page. Therefore, this method runs normally. However, if you do not follow the above method, write the following code (UseRegisterclientscriptblockMethod ):
Page. clientscript. registerclientscriptblock (Me. GetType (), "testing", _ "document. Forms [0] ['textbox1']. Focus ();", true)
The text box control does not get the focus and generates a javascript error on the page.
Example:
// Add a script at the top of the page
If ( ! Page. clientscript. isclientscriptblockregistered ( " Myscriptkey " )) // Determine if the same already exists // Key Value scriptblock
{
String Myscript = @" Function alerthello () {alert ('Hello xuanhun !! ');} " ; // Script content
Page. clientscript. registerclientscriptblock ( This . GetType (), " Myscriptkey " , Myscript, True );
}
// Add a script at the bottom of the page
If ( ! Page. clientscript. isstartupscriptregistered ( " Footscriptkey " ))
{
String Callscript = @" Alerthello (); " ;
Page. clientscript. registerstartupscript ( This . GetType (), " Footscriptkey " , Callscript, True );
}
// Add references to JS files
If ( ! Page. clientscript. isclientscriptincluderegistered ( " Incluedekey " ))
{
String Includescript = @" /JS/showhello. js " ;
Page. clientscript. registerclientscriptinclude ( " Includekey " , Includescript );
}