3 ways to output JavaScript scripts in the ASP. NET CS file and the difference

Source: Internet
Author: User
Tags httpcontext

The difference between Response.Write and Page.ClientScript.RegisterStartupScript and Page.ClientScript.RegisterClientScriptBlock

Method 1, using Response.Write, this method will write the JS code at the top of the page (System.Web.HttpContext.Current.Response.Write ("<script language=javascript>alert (' JS code ');</script>" );

Method 2, using RegisterStartupScript, this method will embed the JS code at the bottom of the page, the end of the form (</form>), for the JS code to run after the page control is loaded:
System.Web.UI.Page Page = (System.Web.UI.Page) System.Web.HttpContext.Current.Handler;
if (!page. Clientscript.isstartupscriptregistered (page. GetType (), "ClientScript"))
Page. Clientscript.registerstartupscript (page. GetType (), "ClientScript", "<script language=javascript>alert (' JS code ');</script>");

Method 3, using RegisterClientScriptBlock, this method will embed the JS code at the top of the page, the first (<form>) of the form, for the JS code to execute before the control is loaded, Similar to the Response.Write method above:
System.Web.UI.Page Page = (System.Web.UI.Page) System.Web.HttpContext.Current.Handler;
if (!page. Clientscript.isclientscriptblockregistered (page. GetType (), "ClientScript"))
Page. Clientscript.registerclientscriptblock (page. GetType (), "ClientScript", "<script language=javascript>alert (' JS code ');</script>");

So what is the difference between method 2 and Method 3? The main difference is that the RegisterStartupScript method is to embed JavaScript at the bottom of an ASP. NET page, just before the close element </form>. The RegisterClientScriptBlock method is to embed JavaScript in the page to open the element <form> close behind. So, what's the difference? As we are going to see, this is a big difference.


In this example, here's how to place focus on a text box on a page when the page is loaded into the browser-using Visual Basic that leverages the RegisterStartupScript method:

<span style= "Color:rgb (255, 0, 0); >page.clientscript.registerstartupscript (Me.GetType (), "testing", _ "document.forms[0][' TextBox1 '].focus ();", True) </span>
This method works fine because the text box on the page has been generated and placed on the page when the browser runs to the bottom of the page and executes this little piece of JavaScript. However, if you do not follow the above method, write the following code (using the RegisterClientScriptBlock method):

<span style= "Color:rgb (255, 0, 0); >page.clientscript.registerclientscriptblock (Me.GetType (), "testing", _ "document.forms[0][' TextBox1 '].focus () ; ", True) </span>
The text box control will not have the focus and a JavaScript error will be generated on the page.

3 ways to output JavaScript scripts in the ASP. NET CS file and the difference

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.