Asp.net RegisterStartupScript不';不要使用ScriptManager、Updatepanel。为什么呢? protectedvoid timer1\u Tick(对象发送方,事件参数e) { foreach(rpChat.Items中的RepeaterItem项) { TextBox txt=item.FindControl(“txtChatMessage”)作为TextBox; 如果(txt!=null) { message[i]=txt.Text; 我--; } } lblStatusChat.Text=“”; RepeaterBind(); 字符串javaScript=“\n”+”警报('Button1_单击客户端”);\n“+”; Page.ClientScript.RegisterStartupScript(this.GetType(),“myKey”,javaScript); }

Asp.net RegisterStartupScript不';不要使用ScriptManager、Updatepanel。为什么呢? protectedvoid timer1\u Tick(对象发送方,事件参数e) { foreach(rpChat.Items中的RepeaterItem项) { TextBox txt=item.FindControl(“txtChatMessage”)作为TextBox; 如果(txt!=null) { message[i]=txt.Text; 我--; } } lblStatusChat.Text=“”; RepeaterBind(); 字符串javaScript=“\n”+”警报('Button1_单击客户端”);\n“+”; Page.ClientScript.RegisterStartupScript(this.GetType(),“myKey”,javaScript); },asp.net,updatepanel,scriptmanager,Asp.net,Updatepanel,Scriptmanager,timer\u单击触发器和更新面板。并且警报消息不会显示在timer_tick事件上,您需要使用ScriptManager类,因为您在执行回发和使用updatepanel时正在注册脚本 MSDN: ScriptManager.RegisterStartupScript方法,用于在将控件包装到UpdatePanel中时将客户端脚本添加到页面 ASPX页面 protected void timer1_Tick(object sender, EventArgs e) { for

timer\u单击触发器和更新面板。并且警报消息不会显示在timer_tick事件上,您需要使用ScriptManager类,因为您在执行回发和使用updatepanel时正在注册脚本

MSDN:

ScriptManager.RegisterStartupScript方法,用于在将控件包装到UpdatePanel中时将客户端脚本添加到页面

ASPX页面

protected void timer1_Tick(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in rpChat.Items)
        {
            TextBox txt = item.FindControl("txtChatMessage") as TextBox;
            if (txt != null)
            {
                message[i] = txt.Text;
                i--;
            }
        }
        lblStatusChat.Text = "";
        RepeaterBind();
        string javaScript = "<script language=JavaScript>\n" + "alert('Button1_Click client-side');\n" + "</script>";

        Page.ClientScript.RegisterStartupScript(this.GetType(), "myKey", javaScript);
    }
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:Label ID="lblDisplayDate" runat="server" Text="Label"></asp:Label>
         <asp:Button ID="btnPostback" runat="server" onclick="btnPostback_Click" 
        Text="ClickMe" />
    </ContentTemplate>
</asp:UpdatePanel>
</div>


当您使用UpdatePanel时,就不能像您尝试的那样使用ClientScript调用JavaScript。您必须改用
ScriptManager.RegisterStartupScript

所以改变你的想法

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

    ScriptManager.RegisterStartupScript(btnPostback,this.GetType(), "JSCR", sb.ToString(),false);

}


一定是晚了一点,但我在这里找到了答案


您必须使用System.Web.UI.ScriptManager.RegisterClientScriptBlock而不是Page.ClientScript.RegisterStartupScript

在我的情况下,ScriptManager.RegisterStartupScript也不起作用

不起作用:

ScriptManager.RegisterStartupScript(updatePanelId,updatePanelId.GetType(), "alert", javaScript, true);
ScriptManager.RegisterStartupScript(Me、,
Me.GetType(),
格式(“数据{0}”,Me.ID),
“警惕(111);”,
(错误)
工作:

ScriptManager.RegisterStartupScript(Me.Page,
Me.GetType(),
格式(“数据{0}”,Me.ID),
“警惕(111);”,
(错误)

示例中的
Me
是从
System.Web.UI.WebControls.WebParts.WebPart

继承的自定义控件,我一直使用ASP.NET 2.0,无法提供面板ID,因此无法使用。有什么建议吗?传递更新面板ID和类型是错误的,请将
this.Page
this.GetType()
传递给它。对于在
母版页中注册的脚本,附加的
.Page
也对我有效。
ScriptManager.RegisterStartupScript(updatePanelId,updatePanelId.GetType(), "alert", javaScript, true);
ScriptManager.RegisterStartupScript(Me, 
  Me.GetType(), 
  String.Format("Data{0}", Me.ID), 
  "<script>alert(111);</script>", 
  False)
ScriptManager.RegisterStartupScript(Me.Page, 
  Me.GetType(), 
  String.Format("Data{0}", Me.ID), 
  "<script>alert(111);</script>", 
  False)