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>