Page. ClientScript. RegisterStartupScript () [transferred from Sihai Park http://www.cnblogs.com/sihai6b204 /]

Source: Internet
Author: User
Tags script tag

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

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 a Boolean value of the script flag.

Note:

The STARTUP script is uniquely identified by its key and type. Scripts with the same key and type are considered as repeated scripts. Only scripts of the given type and key pair can be registered on this page. Trying to register a registered script will not create repeated scripts.

Call the IsStartupScriptRegistered method to check whether the startup script with the given key and type pair has been registered. This avoids unnecessary script addition attempts.

In this overload of the RegisterStartupScript method, the addScriptTags parameter can be used to indicate whether the script provided by the script parameter is packaged in<Script>Element block. Set addScriptTagsTrueIndicates that the script flag will be added automatically.

RegisterStartupScriptThe script block added by the method is executed before the page is loaded but the OnLoad event of the page is triggered.

Example

 

<%@ 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>
 

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.