Difference between Response.Write () and ClientScript.RegisterStartupScript ()?

What is the difference between Response.Write () and ClientScript.RegisterStartupScript () Thanks.

+1
source share
4 answers

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.

+4
source

There is a huge difference.

Basically, Response.Write will write to your response stream right now, usually it will put everything that you write on the very top of the output of your page, even before the tag (if you do not call it after the page display event).

When you use RegisterStartupScript, it will wait and write your JavaScript in the response stream after the page controls appear (IE, the controls have written their HTML code in the response stream). This means that the JavaScript you register will be executed by the browser after another HTML before it is loaded into the DOM. This is very similar to an event. Another thing is that if you "register" a script, therefore, if there are several controls on the page that need the JavaScript that they can check in order to check whether it has already been registered, only once once, and both controls use her client side.

Hope this makes sense, then there are more details, but I tried to keep it simple.

+3
source

Response.Write

The Write method writes the given string to the current HTTP output.

ClientScriptManager.RegisterStartupScript

Registers the launch of a script using the Page Object.

0
source

As I think, both of these methods are not related. Response.Write () can be used to write something on the page that is displayed. Although ClientScript.RegisterStartupScript () can be used to register javascript startup on the page.

0
source

Source: https://habr.com/ru/post/1384107/


All Articles