简体   繁体   English

JavaScript中未更新ASP.Net会话

[英]ASP.Net session not updated in javascript

当单击我的按钮时,我启动了javascript以获取会话。但是会话的值未更新...

alert('<%= Session["file"]%>');

It won't be up to date if its changed after the page is rendered. 如果在页面渲染后对其进行了更改,它将不会是最新的。

You might want to look at page methods (an ajax system) or another ajax approach. 您可能想看一下页面方法(一个ajax系统)或另一个ajax方法。

Any inline code once rendered to the page will not change, which is a normal behavior. 一旦呈现到页面上的任何内联代码都不会更改,这是正常现象。 use a Hidden field instead. 请改用“隐藏”字段。

mark-up: 标记:

<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

code-behind: 后台代码:

protected void Button1_Click(object sender, EventArgs e)
{
    Session["file"] = "Data Here";
    HiddenField1.Value = Session["file"].ToString();
}

javascript: javascript:

alert(document.getElementById('<%= HiddenField1.ClientID %>').value);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM