会话 ASP.NET

发布于 2024-12-16 00:37:06 字数 799 浏览 2 评论 0原文

asp.net/c# 中的会话问题

我有多个 asp.net 页面和一个类对象。 例如 类 User 对象包含

    public string Name { set; get; }
    public string Address { set; get; }
    etc.....

pg1.aspx:当单击按钮时,数据存储在会话中并重定向到 pg2.aspx,

例如pg1.aspx

    User u = new User();
    u.Name = TextBox1.Text;
    Session.Add("USERINFO", u);
    Response.Redirect("pg2.aspx");

例如pg2.aspx //读取数据..

    User u = ((User)Session["USERINFO"]);
    Response.Write(u.Name + "<br/>");

..等..(我有5页,数据在会话中从一页传递到另一页)一旦用户到达它存储在数据库中的最后一页,否则我不希望在数据库中存储不完整的数据。

一切正常,除非我使用多个选项卡运行同一个网络应用程序,数据会相互覆盖...如果我使用单独的浏览器,但不使用选项卡,则一切正常...

我怎样才能避免用户在中输入数据tab1 转到第 3 页,如果用户打开 tab2 并且继续不超过 tab1 中输入的数据...

我希望这是有道理的...我正在努力完成的任务...谢谢。

Session question in asp.net/c#

I have multiple asp.net pages and a class object.
e.g.
Class User object contains

    public string Name { set; get; }
    public string Address { set; get; }
    etc.....

pg1.aspx: when button is clicked, the data is stored in the session and redirects to pg2.aspx

e.g. pg1.aspx

    User u = new User();
    u.Name = TextBox1.Text;
    Session.Add("USERINFO", u);
    Response.Redirect("pg2.aspx");

e.g. pg2.aspx //reading the data..

    User u = ((User)Session["USERINFO"]);
    Response.Write(u.Name + "<br/>");

..etc.. ( I have 5 pages and the data is passed around from page to page in a session) once the user reaches the last page it stores in the database otherwise I dont want to store incomplete data in the database.

It all works fine except if I use multiple tabs to run same web app, the data overwrites eachother... It works fine if I use separate browsers, but not with tabs...

How can I avoid a user to enter the data in tab1 get to page 3 and if the user opens tab2 and continues not to over the data was entered in tab1....

I hope it makes sense... what I am trying to accomplish... Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

¢蛋碎的人ぎ生 2024-12-23 00:37:06

这可能是一个重复的问题,所以我将简单地将您重定向到一些可以解决您的问题的答案。

不幸的是,接受的答案不起作用,因为它为每个页面维护一个“唯一”会话标识符,而不是您的 5 个页面组。

您的浏览器版本以及在其中启动新选项卡和窗口的不同方法决定了会话是否在多个选项卡和窗口之间共享。可以通过

  • 链接文章中描述的对 web.config 的修改
  • 来控制它为每个会话值提供唯一的会话密钥前缀
  • 使用某种逻辑来防止同一用户在同一台​​计算机上打开多个会话

This is probably a duplicate question, so I'll simply redirect you to some answers that would solve your problem.

Unfortunately, the accepted answer won't work since it maintains a "unique" session identifier for each page, not your group of 5 pages.

Your browser version and the different methods of launching new tabs and windows in it determines whether sessions are shared across multiple tabs and windows. Controlling it could be done with

  • The modification to web.config as described in the linked article
  • Coming up with a unique session key prefix for each session value
  • Using some logic to prevent the same user from opening multiple sessions on the same machine
南…巷孤猫 2024-12-23 00:37:06

解决当前实现的最简单方法可能是检查您是否拥有您期望从该页面获得的信息,如果不满足条件,则需要输入数据,或者根据具体情况适当重定向您所处流程的阶段(基于可用的数据)。

因此,除了添加到会话之外,您还可以获得:

var value = Session[key] as string;
if (value != null)
{
    //move on to the next appropriate page 
    //after determining which that is by other conditions
}
//we obviously need the page at which we get this data!

并且您可以从那里建立您的条件

The simplest way to work around your current implementation would likely be to check whether or not you have the information you would expect from that page, and if the conditions aren't met then require the data to be input, or redirect appropriately depending on which stage in the process you are (based on the data that is available).

So, as well as adding to the session, you can get:

var value = Session[key] as string;
if (value != null)
{
    //move on to the next appropriate page 
    //after determining which that is by other conditions
}
//we obviously need the page at which we get this data!

And you can build upon your conditions from there

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文