The Response.Write method can be used to output code during the page rendering phase. The server tag <%= %> is the shortcut for <%Response.Write( )%> .
If you use Response.Write from the code behind, you will write to the page before it starts rendering, so the code will be outside the html document. Although the browser executes the code, it does not work properly. Having something in front of the doctype tag will cause the browser to ignore doctype and display the page in quirks mode, which usually breaks the layout. In addition, since the script runs before any of the pages exists, the code cannot access any elements on the page.
The ClientScript.RegisterStartupScript method is the preferred way to dynamically add a script to a page. It will display the script at the end of the form so that it does not break the html documnet and cannot access the elements in the form.
In addition, you give each script an identifier, which means that duplicates are deleted. If a user control registers a script and you use multiple instances of the user control, the script will only appear once per page.
source share