The difference between RegisterClientScriptBlock and RegisterStartupScript

Source: Internet
Author: User
RegisterClientScriptBlock, RegisterStartupScript are all registered as a script block.

Clientscript.registerclientscriptblock (this. GetType (), "SCRIPT1", "alert (1);", true);
Clientscript.registerstartupscript (this. GetType (), "SCRIPT1", "alert (2);", true);

The difference is that RegisterClientScriptBlock is registered at the front of the body, while RegisterStartupScript is registered in the body of the last face.

Why is that? Some of the optimized performance of the article said that the script should be placed at the end of the body load, but according to the actual situation of some applications, the script must be loaded initially, then you have to use Clientscript.registerstartupscript.

Tell me about their second parameter, key.

The second parameter key is used to avoid duplicate registration, the above code, their key is script1, but does not conflict, because the method name is not the same.

The following code is RegisterStartupScript, the second sentence will not have any output, because you have previously registered a SCRIPT1 script.

Clientscript.registerstartupscript (this. GetType (), "SCRIPT1", "alert (2);", true);
Clientscript.registerstartupscript (this. GetType (), "SCRIPT1", "alert (3);", true);
Clientscript.registerstartupscript (this. GetType (), "Script2", "alert (4);", true);

Although Response.Write can output JavaScript, the output is before

ClientScript can easily manage JavaScript, should say Clientscript.registerclientscriptblock and Clientscript.registerstartupscript The only difference is that RegisterClientScriptBlock writes the script code after <form>, and RegisterStartupScript writes the code before </form> (note the end tag).

public void RegisterClientScriptBlock (type type, string key, String script)
public void RegisterClientScriptBlock (type type, string key, String script, bool addscripttags)
public void RegisterStartupScript (type type, string key, String script)
public void RegisterStartupScript (type type, string key, String script, bool addscripttags)

You can see that the syntax is the same.

Type of the startup script to register.

Key to register the startup script, which is the name you gave to the script yourself. Scripts of the same key are treated as duplicates, and for such scripts only output is first registered, the same key in Clientscriptblock and Startupscript is not duplicated.

Script scripting code.

addScriptTags if the <script> tag is added, if the script code does not include a <script> tag, you should specify TRUE if the value is not specified and will be treated as false.

Clientscript.registerclientscriptblock (this. GetType (), "Key1", @ "function Go"
{
Alert (');
} ", true);
Clientscript.registerstartupscript (this. GetType (), "Key1", "Go ();", true);

  • 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.