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?
- <SPAN style="Font-family:microsoft Yahei"><%@ 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 ">
- <HTML xmlns="http://www.w3.org/1999/xhtml">
- <head runat="Server">
- <title></title>
- <script type="Text/javascript">
- function GetValue () {
- var value = document.getElementById ("test"). Value;
- alert (value);
- }
- </Script>
- </head>
- <body>
- <form id="Form1" runat="Server">
- <div>
- <input type="text" value="value" id="test"/>
- </div>
- </form>
- </body>
- </html>
- </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?
- <span style="Font-family:microsoft Yahei" > protected void Page_Load (object sender, EventArgs e) /c4>
- {
- if (! IsPostBack) {
- Page.ClientScript.RegisterClientScriptBlock (This. GetType (), "msg", "<script>alert (' RegisterClientScriptBlock ') </script>");
- Page.ClientScript.RegisterStartupScript (This. GetType (), "msg", "<script>alert (' RegisterStartupScript ') </script>");
- }
- }</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