What is the difference between Response.Write() and ClientScript.RegisterStartupScript()
Thank you.
What is the difference between Response.Write() and ClientScript.RegisterStartupScript() Thank you.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
Response.Writemethod can be used to output code during the rendering phase of the page. The<%= %>server tag is a shortcut for<%Response.Write( )%>.If you use
Response.Writefrom the code behind, you will write to the page before it has started rendering, so the code will end up outside the html document. Eventhough the browser will execute the code, it doesn’t work properly. Having something before the doctype tag will make the browser ignore the doctype and render the page in quirks mode, which usually breaks the layout. Also, as the script runs before anything of the page exists, the code can’t access any elements in the page.The
ClientScript.RegisterStartupScriptmethod is the preferred way of adding script dynamically to the page. It will render the script at the end of the form so that it doesn’t break the html documnet, and it can access the elements in the form.Also, you give each script an identity, which means that duplicates are removed. If a user control registers a script, and you use several instances of the user control, the script will only be rendered once in the page.