RegisterStartupScript和RegisterClientScriptBlock之间的区别?是唯一的区别RegisterStartupScript和RegisterClientScriptBlock是的RegisterStartupScript提出的JavaScript结束前</form>的页面的标签,并把的RegisterClientScriptBlock它开始之后<form>的页面的标签?另外,你什么时候选择一个?我写了一个快速的示例页面,我遇到了一个问题,我不确定它发生原因的确切原因。这是aspx标记:<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblDisplayDate" runat="server"
Text="Label" /><br />
<asp:Button ID="btnPostback" runat="server"
Text="Register Startup Script"
onclick="btnPostback_Click" /><br />
<asp:Button ID="btnPostBack2" runat="server"
Text="Register"
onclick="btnPostBack2_Click" />
</div>
</form>
</body></html>这是守则背后:protected void Page_Load(object sender, EventArgs e){
lblDisplayDate.Text = DateTime.Now.ToString("T");}protected void btnPostback_Click(object sender, EventArgs e){
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(@"<script language='javascript'>");
sb.Append(@"var lbl = document.getElementById('lblDisplayDate');");
sb.Append(@"lbl.style.color='red';");
sb.Append(@"</script>");
if(!ClientScript.IsStartupScriptRegistered("JSScript"))
{
ClientScript.RegisterStartupScript(this.GetType(),"JSScript",
sb.ToString());
}}protected void btnPostBack2_Click(object sender, EventArgs e){
}问题是,当我单击btnPostBack按钮时,它会进行回发并将标签更改为红色,但是当我单击btnPostBack2它时,它会进行回发,但标签颜色不会变为红色。为什么是这样?是因为标签没有初始化?我还读到,如果你正在使用UpdatePanel,你需要使用ScriptManager.RegisterStartupScript,但如果我有MasterPage,我会使用ScriptManagerProxy吗?
添加回答
举报
0/150
提交
取消