您的位置:首页 > 其它

Page.ClientScript.RegisterStartupScript用法小结

2014-11-30 16:56 471 查看
使用类型、键、脚本文本和指示是否添加脚本标记的布尔值向Page对象注册启动脚本。

参数

type
要注册的启动脚本的类型。

key
要注册的启动脚本的键。

script
要注册的启动脚本文本。

addScriptTags
指示是否添加脚本标记的布尔值.

备注:

启动脚本由它的键和类型唯一标识。具有相同的键和类型的脚本被视为重复脚本。只有使用给定的类型和键对的脚本才能使用该页面进行注册。试图注册一个已经注册的脚本不会创建重复的脚本。

调用IsStartupScriptRegistered方法以确定具有给定的键和类型对的启动脚本是否已经注册,从而避免不必要的添加脚本尝试。

RegisterStartupScript方法的此重载中,使用addScriptTags参数可指示script参数中提供的脚本是否包装在<script>元素块中。将addScriptTags设置为true指示脚本标记将自动添加。

RegisterStartupScript方法添加的脚本块在页面加载完成但页面的OnLoad事件引发之前执行。

示例

<%@PageLanguage="C#"%>
<scriptrunat="server">
publicvoidPage_Load(Objectsender,EventArgse)
{
//Definethenameandtypeoftheclientscriptsonthepage.
Stringcsname1="PopupScript";
Stringcsname2="ButtonClickScript";
Typecstype=this.GetType();
//GetaClientScriptManagerreferencefromthePageclass.
ClientScriptManagercs=Page.ClientScript;
//Checktoseeifthestartupscriptisalreadyregistered.
if(!cs.IsStartupScriptRegistered(cstype,csname1))
{
Stringcstext1="alert('HelloWorld');";
cs.RegisterStartupScript(cstype,csname1,cstext1,true);
}
//Checktoseeiftheclientscriptisalreadyregistered.
if(!cs.IsClientScriptBlockRegistered(cstype,csname2))
{
StringBuildercstext2=newStringBuilder();
cstext2.Append("<scripttype=text/javascript>functionDoClick(){");
cstext2.Append("Form1.Message.value='Textfromclientscript.'}</");
cstext2.Append("script>");
cs.RegisterClientScriptBlock(cstype,csname2,cstext2.ToString(),false);
}
}
</script>
<html>
<head>
<title>ClientScriptManagerExample</title>
</head>
<body>
<formid="Form1"
runat="server">
<inputtype="text"id="Message"><inputtype="button"value="ClickMe"onclick="DoClick()">
</form>
</body>
</html>

//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);

小技巧:

后台:

protectedvoidButton1_Click(objectsender,EventArgse)
{
stringtemp=Request["he"].ToString();//通过request[控件name(非id)]来引用前台的input控件
Page.ClientScript.RegisterStartupScript(this.GetType(),"js","alert('hello"+temp+"')",true);//alert添加参数
}

前台:

<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title></title>
</head>
<body>
<formid="form1"runat="server">

<fieldset>
<legend>
健康信息
</legend>
高度:<inputname="he"type="text"/>
体重:<inputtype="text"/>
</fieldset>
<asp:ButtonID="Button1"runat="server"Text="Button"onclick="Button1_Click"/>
</form>
</body>
</html>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: