在 asp.net 中保留页面加载后的会话变量

本文关键字:加载 会话 变量 asp net 保留 | 更新日期: 2023-09-27 18:37:15

我试图通过创建会话在页面重新加载后将所选值保留在下拉框中,但我不确定我的逻辑是否有效,因为下拉列表在刷新之前没有保留所选值。这是我所拥有的:

string txtDDLLocation = ddlLocation.SelectedValue;
Session["MySessionVar"] = txtDDLLocation;
Page.Response.Redirect(Page.Request.Url.ToString(), true);
ddlLocation.SelectedValue = (string)Session["MySessionVar"];

在 asp.net 中保留页面加载后的会话变量

您在Response.Redirect之后调用ddlLocation.SelectedValue = (string)Session["MySessionVar"]

确保在事件page_load加载值

protected void page_load(object sender, EventArgs e)
{
if(Session["MySessionVar"]!=null)
   ddlLocation.SelectedValue = (string)Session["MySessionVar"]
}

重定向代码后的 lol 设置会话将永远不会到达会话变量