我的网站上有以下代码:
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel runat="server" id="UpdatePanel" updatemode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger controlid="myButton_btn" eventname="Click" />
</Triggers>
<ContentTemplate>
<script>
function pageLoad(){
window.alert("<%=Session("myVariable")%>");
}
}
</script>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="myButton_btn" Text="Prev Month" runat="server"></asp:Button>
每次单击按钮时,javascript 都会运行。但是,即使我的子例程在单击按钮后更新了 session 变量,我总是会收到相同的警报。换句话说,相同的 javascript 在回发后运行,并且不会更新。我肯定 session 变量正在更改。有谁知道为什么会这样?感谢您的意见!
非常感谢!
最佳答案
在按钮处理程序中试试这个:
VB.Net
ScriptManager.RegisterStartupScript(Me.UpdatePanel, GetType(string), "alertScript", String.Format("alert('{0}');",Session("myVariable")), true)
C#
ScriptManager.RegisterStartupScript(this.UpdatePanel, typeof(string), "alertScript", string.Format("alert('{0}');", Session["myVariable"]), true);
关于javascript - 在 ASP.NET 中异步回发后更新 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4900522/