Page.ClientScript.RegisterStartupScript
Page.ClientScript.RegisterStartupScript(type,
Page.ClientScript.RegisterStartupScript(type,key,script,addScriptTags);
type
要注册的启动脚本的类型。
key
要注册的启动脚本的键。
script
要注册的启动脚本文本。
addScriptTags
指示是否添加脚本标记的布尔值.
追问
是否能够再详细一些,最好可以举例说明或着对我那些参数的常量进行说明。
追答
Page.ClientScript.RegisterStartupScript(this.GetType(), "y", "alert('" + "传成功" + "');", true);
this.GetType() -----在本页面添加脚本
“y”-------------脚本的名字叫y
"alert('" + "传成功" + "');" -----------脚本的内容是alert(“传成功”)
true -----------脚本返回true
使用类型、键、脚本文本和指示是否添加脚本标记的布尔值向
使用类型、键、脚本文本和指示是否添加脚本标记的布尔值向 Page 对象注册启动脚本。
参数
type
要注册的启动脚本的类型。
key
要注册的启动脚本的键。
script
要注册的启动脚本文本。
addScriptTags
指示是否添加脚本标记的布尔值.
备注:
启动脚本由它的键和类型唯一标识。具有相同的键和类型的脚本被视为重复脚本。只有使用给定的类型和键对的脚本才能使用该页面进行注册。试图注册一个已经注册的脚本不会创建重复的脚本。
调用 IsStartupScriptRegistered 方法以确定具有给定的键和类型对的启动脚本是否已经注册,从而避免不必要的添加脚本尝试。
在 RegisterStartupScript 方法的此重载中,使用 addScriptTags 参数可指示 script 参数中提供的脚本是否包装在 <script> 元素块中。将 addScriptTags 设置为 true 指示脚本标记将自动添加。
RegisterStartupScript 方法添加的脚本块在页面加载完成但页面的 OnLoad 事件引发之前执行。
[csharp] view plaincopyprint?
示例
<%@ 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>
<html>
<head>
<title>ClientScriptManager Example</title>
</head>
<body>
<form id="Form1"
runat="server">
<input type="text" id="Message"> <input type="button" value="ClickMe" onclick="DoClick()">
</form>
</body>
</html>
[csharp] view plaincopyprint?
//ASP.NET后台页面跳转
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>if(confirm('保存成功!是否继续添加?')){location.href='ProductonAdd.aspx'}else{location.href='ProductonList.aspx'}</script>");
//后台弹出确定框
ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('请正确输入!');</script>");
//ASP.NET后台页面跳转
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>alert('数据添加成功!');{location.href='ProductonList.aspx'}</script>");
或
Page.ClientScript.RegisterStartupScript(typeof(string), "", "<script>window.location.href='AdminMain.aspx';</script>");
//后台弹出文本框
ScriptManager.RegisterStartupScript(Page, typeof(string), "popUp", "window.open('rptView.aspx','打印预览','toolbar=no,location=no,scrollbars=yes,top=200px,left=200px,width=904px,height=650px')", true);
小技巧:
[csharp] view plaincopyprint?
后台:
protected void Button1_Click(object sender, EventArgs e)
{
string temp = Request["he"].ToString();//通过request[控件name(非id)]来引用前台的input控件
Page.ClientScript.RegisterStartupScript(this.GetType(), "js", "alert('hello" + temp + "')", true);//alert添加参数
}
前台:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<fieldset>
<legend>
健康信息
</legend>
高度:<input name="he" type="text"/>
体重: <input type="text" />
</fieldset>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</form>
</body>
</html>
Page.ClientScript.RegisterStartupScript