我有一个索引页面,它将用户发送到单独的浏览器选项卡上的编辑产品页面。
对于编辑的每个产品,索引都会重写 Session["ProductID"]。
然后,编辑页面包含以下代码,以便为该选项卡和产品提供唯一标识符:
if (!IsPostBack) //first time page load
{
Random R = new Random(DateTime.Now.Millisecond + DateTime.Now.Second * 1000 + DateTime.Now.Minute * 60000 + DateTime.Now.Minute * 3600000);
PageID.Value = R.Next().ToString();
Session[PageID.Value + "ProductID"] = Session["ProductID"];
}
Run Code Online (Sandbox Code Playgroud)
这是有效的,当同一用户打开多个选项卡时,我只在代码中引用 Session[PageID.Value + "ProductID"],以便我始终拥有正确的 ID。(我正在一个受信任的环境中工作,这是针对内部网的,因此我不太关心安全级别)。
如果用户通过按 F5 键刷新页面,就会出现我的问题。此时 Session[PageID.Value + "ProductID"] 获取他打开的最后一个产品的 Session["ProductID"]。
例如:
用户 1 在选项卡 1 中打开产品 1
用户 1 在选项卡 2 中打开产品 2
每当他们正常使用该工具时,一切都会正常。但是如果:
产品 1 页面上的用户 1 点击刷新按钮 (F5),产品 1 页面变为产品 2 页面
有没有办法从“第一次加载/从另一个页面重定向”中检测页面刷新,以便我可以告诉我的页面不要更新我的会话[PageID.Value +“ProductID”]?
我通过存储两个版本的状态识别参数解决了一个非常相似的问题:一个在会话中,一个在 ViewState 或 URL (QueryString) 中。
如果您比较 Page_Load 上的两个值,就会告诉您自页面首次加载以来会话变量是否已更改。这应该正是您所需要的。
编辑:代码的粗略草图(警告 - 自从我三年前编写以来还没有看到实际的代码):
protected string currentProductID
{
get
{
return Request.QueryString["ProductID"];
//or:
//return (string)ViewState["ProductID"];
//or:
//return HiddenField1.Value;
}
set
{
Response.Redirect(ResolveUrl("~/MyPage.aspx?ProductID=" + value));
//or:
//ViewState.Add("ProductID", value);
//or:
//HiddenField1.Value = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
//If the problem only occurs when not posting back, wrap the below in
// an if(!IsPostBack) block. My past issue occurred on both postbacks
// and page refreshes.
//Note: I'm assuming Session["ProductID"] should never be null.
if (currentProductID == null)
{
//Loading page for the first time.
currentProductID = (string)Session["ProductID"];
}
else if (currentProductID != Session["ProductID"])
{
//ProductID has changed since the page was first loaded, so react accordingly.
//You can use the original ProductID from the first load, or reset it to match the one in the Session.
//If you use the earlier one, you may or may not want to reset the one in Session to match.
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,请注意,对 ViewState 的更改(包括隐藏控件的值)只会在下一次 PostBack 时生效。刷新后,它们将恢复为最新值。就我而言,这就是我想要的,但听起来它不太适合您的情况。不过,这些信息可能对您有用,具体取决于您如何实现这一点。
我省略了currentProductID与进行比较的讨论Session[PageID.Value + "ProductID"],因为我已经发布了很多代码,并且我不知道您想要做什么的细节。但是,您可以通过多种方式使用 Session、ViewState 和 QueryString 来收集有关页面状态和历史记录的信息。
希望这能给您一个总体思路。如果这还不足以让您继续前进,请告诉我。
| 归档时间: |
|
| 查看次数: |
6541 次 |
| 最近记录: |