我想使用服务器端代码在ASP.Net Web表单应用程序的更新面板中使用一些javascript代码。问题是以下代码仅在我在更新面板外部使用时才起作用,但在内部不起作用,即使有警报。
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Server" OnClick="Button1_Click"/><br />
<asp:Button ID="Button2" runat="server" Text="Cleint" OnClientClick="alert('Hello World(Cleint)')"/>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
背后的代码
protected void Button1_Click(object sender, EventArgs e)
{
this.ClientScript.RegisterStartupScript(this.GetType(),
"ShowMessage", string.Format("<script type='text/javascript'>alert('{0}')</script>",
"Hello World (Server)"));
}
运行此应用程序时,只有Client Click事件有效,但不是服务器端事件。但是,当删除更新面板时,再次尝试两个事件都有效。 我搜索了很多,发现了一些类似的问题,但所有的答案都没有用。我只是想解决上面提到的例子的问题。感谢
答案 0 :(得分:1)
尝试使用以下方法替换按钮点击事件中的代码:
ScriptManager.RegisterStartupScript(this, this.GetType(), this.ClientID, string.Format("alert('{0}')", "Server"), true);
希望这有帮助。