Page
We all know that there are many ways to output content to the client, and the beginner will use respone. Write (string ). For example:
Respone. Write ("Hello word !");
Or output JS
Respone. Write ("<SCRIPT type = 'text/JavaScript '> alert ('Hello word! '); </SCRIPT> ");
However, when you view the client source code, you will find that the output content is displayed at the front end of the source code. Obviously, it destroys the HTML format, in some cases, this will affect the page layout and other effects.
The correct output method should be: This. clientscript. registerstartupscript or this. clientscript. registerclientscriptblock.
This. clientscript. registerstartupscript is the first line of registration script at the beginning of form. The latter is the registration script at the end of form. In this way, the HTML format will not be damaged, for example:
This. clientscript. registerstartupscript (this. GetType (), "scriptkey", "<SCRIPT type = 'text/JavaScript '> alert ('Hello word! '); </SCRIPT> ")
Or
This. clientscript. registerstartupscript (this. GetType (), "scriptkey", "alert ('Hello word! '); ", True)
This. clientscript. registerclientscriptblock is similar.
Updatepanel
When you want to output a piece of js in updatepanel, the above method will not achieve the expected results. Take a look at the example.
There is an updatepanel ID that is uppn
Scriptmanager. registerclientscriptblock (uppn, this. GetType (), "scriptkey", "alert ('Hello word! '); ", True)
Or
Scriptmanager. registerstartupscript (uppn, this. GetType (), "scriptkey", "alert ('Hello word! '); ", True)
In this case, when the updatepanel content is loaded to the client, "Hello word!" will pop up !" Dialog box.
In this way, it is more convenient to output JS from the background.