The difference between RegisterClientScriptBlock and RegisterStartupScript

Source: Internet
Author: User

RegisterClientScriptBlock the client script immediately after the start tag of the element of the Page object, RegisterStartupScript emits the script before the end tag of the element of the Page object. If your script has statements that interact with the Page object (the Doucument object), it is recommended to use RegisterStartupScript, whereas if you want the client script to execute as early as possible, You can use RegisterClientScriptBlock and Response.Write.

We create a new default page:

[HTML]View Plaincopyprint?
  1. <SPAN style="Font-family:microsoft Yahei"><%@ page language="C #"   autoeventwireup="true" codebehind="Default.aspx.cs" inherits="study._default"%>
  2. <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
  3. <HTML xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="Server">
  5. <title></title>
  6. <script type="Text/javascript">
  7. function GetValue () {
  8. var value = document.getElementById ("test"). Value;
  9. alert (value);
  10. }
  11. </Script>
  12. </head>
  13. <body>
  14. <form id="Form1" runat="Server">
  15. <div>
  16. <input type="text" value="value" id="test"/>
  17. </div>
  18. </form>
  19. </body>
  20. </html>
  21. </SPAN>
<%@ page language= "C #" autoeventwireup= "true" codebehind= "Default.aspx.cs" inherits= "Study._default"%><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">


Then we register the next two scripts in the background pageload event:

[CSharp]View Plaincopyprint?
  1. <span style="Font-family:microsoft Yahei" > protected void Page_Load (object sender, EventArgs e) /c4>
  2. {
  3. if (! IsPostBack) {
  4. Page.ClientScript.RegisterClientScriptBlock (This.  GetType (), "msg", "<script>alert (' RegisterClientScriptBlock ') </script>");
  5. Page.ClientScript.RegisterStartupScript (This.  GetType (), "msg", "<script>alert (' RegisterStartupScript ') </script>");
  6. }
  7. }</span>
protected void Page_Load (object sender, EventArgs e)        {            if (! IsPostBack) {                Page.ClientScript.RegisterClientScriptBlock (this. GetType (), "msg", "<script>alert (' RegisterClientScriptBlock ') </script>");                Page.ClientScript.RegisterStartupScript (this. GetType (), "msg", "<script>alert (' RegisterStartupScript ') </script>");            }        }

Run the page we can clearly see the registration location of two scripts, registerclientscriptblock after the <form> tag, and RegisterStartupScript before the </form> tag.

So if we use RegisterClientScriptBlock to get the value on the page before the page is completely loaded, we can't get it.

The difference between RegisterClientScriptBlock and RegisterStartupScript

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.