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

后端 未结 4 944
悲&欢浪女
悲&欢浪女 2020-12-17 05:52

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

4条回答
  •  半阙折子戏
    2020-12-17 06:31

    There is a huge difference.

    Basically Response.Write will write to your response stream right now, normally this will put whatever you write at the very top of your page output, even before the tag (unless you call it after the page render event).

    When you use RegisterStartupScript it will wait and write your JavaScript to the response stream after the page's controls have rendered (IE, the controls wrote their HTML to the response stream). This means the JavaScript you register will be executed by the browser after the other HTML before it has been loaded into the DOM. This is very similar to the event. Another thing this does is if “registers” the script so if you have more than one control on the page that both need that JavaScript they can check to see if it’s already been registered so it’s only rendered once and both controls use it client side.

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

提交回复
热议问题