我有一个索引页,它把用户送到一个编辑产品页上的单独的浏览器标签。
对于每个编辑过的产品,索引会重写会话“ProductID”。
然后,Edit页面将具有以下代码,以使此选项卡和产品具有唯一标识符:
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"];
}这是可行的,当同一个用户打开多个选项卡时,我只在代码中引用SessionPageID.Value + "ProductID“,这样我就始终拥有正确的ID。(我在一个受信任的环境中工作,这是针对内部网的,因此我不太担心安全级别)。
如果用户通过按F5键进行页面刷新,就会出现我的问题。在这一点上,SessionPageID.Value + "ProductID“将获得他打开的最后一个产品的会话”ProductID“。
例如:
用户1在tab1中打开product1
用户1在tab2中打开product2
只要他们正常使用该工具,一切都会正常工作。但是,如果:
product1页面上的用户1单击刷新按钮(F5) product1页面将变为product2页面
有没有办法检测“第一次从另一个页面加载/重定向”的页面刷新,这样我就可以告诉我的页面不要更新我的SessionPageID.Value + "ProductID“
发布于 2011-01-26 04:57:54
我通过存储状态标识参数的两个版本解决了一个非常类似的问题:一个在会话中,另一个在ViewState或URL (QueryString)中。
如果比较Page_Load上的这两个值,就会知道自页面首次加载以来会话变量是否发生了更改。这应该正是您所需要的。
编辑:代码的粗略草图(警告-自从我3年前写了代码以来,还没有见过实际的代码):
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.
}
}在上面的代码中,请注意对ViewState所做的更改(包括隐藏控件的值)仅在下一个PostBack上生效。在刷新时,它们将恢复为最近的值。在我的情况下,这是我想要的,但听起来不太适合你的情况。尽管如此,这些信息可能对您有用,这取决于您如何实现它。
我忽略了将currentProductID与Session[PageID.Value + "ProductID"]进行比较的讨论,因为我已经发布了很多代码,而我不知道您要做什么的细节。但是有多种方法可以使用会话、ViewState和QueryString来收集有关页面状态和历史记录的信息。
希望这能给你一个大概的概念。如果这还不足以让你继续下去,请告诉我。
发布于 2011-01-26 04:30:39
就我个人而言,我会选择URL参数。例如,将产品ID作为URL参数传递。
如果你需要没有参数的页面,你可以例如
这样你就可以区分第一次调用(=参数存在)和second+调用(参数不存在)。
发布于 2011-01-26 04:31:32
你可能想看看this。我觉得它和你要找的很接近。
https://stackoverflow.com/questions/4798396
复制相似问题