Add client script RegisterStartupScript to the page dynamically

Source: Internet
Author: User
Document directory
  • In many cases, you can create a client script for the page by declaring it. This script is usually used as a script block. However, it is sometimes necessary to dynamically create client scripts. This method is useful if the value in the script or script depends only on the available information on the server. For example, you may want to insert a client script to a page that references a server control with an unknown name (ID) you can only know when running the application.
  • ClientScriptManager. RegisterStartupScript method (Type, String, String, Boolean)
In many cases, you can create a client script for the page by declaring it. This script is usually used as a script block. However, it is sometimes necessary to dynamically create client scripts. This method is useful if the value in the script or script depends only on the available information on the server. For example, you may want to insert a client script to a page that references a server control with an unknown name (ID) you can only know when running the application. ClientScriptManager. RegisterStartupScript method (Type, String, String, Boolean)

Register the startup script with the page Object using the type, key, script text, and Boolean value indicating whether to add the script tag.

Namespace:System. Web. UI
Assembly:System. Web (in system. web. dll)

 

C # syntax

public void RegisterStartupScript (

    Type type,

    string key,

    string script,

    bool addScriptTags

)

Parameters

type

The type of the startup script to be registered.

 

key

The key of the startup script to be registered.

 

script

The startup script Text to be registered.

 

addScriptTags

Indicates whether to add

Example

The following code example demonstratesRegisterStartupScriptMethod usage. Note that the addScriptTags parameter is setFalseThe script start and end tags are included in the script parameters.

<%@ Page Language="C#"%>

<script runat="server">

  public void Page_Load(Object sender, EventArgs e)

  {

    // Define the name and type of the client scripts on the page.

    String csname1 = "PopupScript";

    String csname2 = "ButtonClickScript";

    Type cstype = this.GetType();

        

    // Get a ClientScriptManager reference from the Page class.

    ClientScriptManager cs = Page.ClientScript;

 

    // Check to see if the startup script is already registered.

    if (!cs.IsStartupScriptRegistered(cstype, csname1))

    {

      String cstext1 = "alert('Hello World');";

      cs.RegisterStartupScript(cstype, csname1, cstext1, true);

    }

 

    // Check to see if the client script is already registered.

    if (!cs.IsClientScriptBlockRegistered(cstype, csname2))

    {

      StringBuilder cstext2 = new StringBuilder();

      cstext2.Append("<script type=text/javascript> function DoClick() {");

      cstext2.Append("Form1.Message.value='Text from client script.'} </");

      cstext2.Append("script>");

      cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false);

    }

  }

</script>

  

    <title>ClientScriptManager Example</title>

  

  <body>

     <form id="Form1"

         runat="server">

        <input type="text" id="Message"> <input type="button" value="ClickMe" onclick="DoClick()">

     </form>

  </body>

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.