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>