asp.net 在回发之前设置会话

发布于 2024-12-17 17:23:43 字数 1559 浏览 2 评论 0原文

有没有一种简单的方法可以在回发开始之前立即将所有需要的全局变量存储在会话中?或者我必须在更改它们的每个步骤中存储它们吗? 我会做类似的事情:

// Global variable.
bool test = true;

// Store all needed information in a session.
protected void Before_globalvariable_is_set_to_default_value(...)
{
    Session["Test"] = test;
    ...
}

protected void Page_Load(object sender, EventArgs e)
{
    if(IsPostBack)
    {
        //if(Session["Test"] != null)
        //{
            test = (bool)Session["Test"];
            Session.Contents.Remove("Test");
        //}
    }
}

这样的事情可能吗?

其他信息

Page_Load (!IsPostBack) 中,我检查用户是否获得更多视觉效果,如果获得,我将全局变量设置为 true。稍后在我的代码中,我检查该 var 是否为 true 并向 GridView 添加其他列。 现在,如果发生 PostBack,我无法检查该 var,因为我丢失了信息。我知道我需要将信息存储在 Session 中。如果我在将全局变量设置为 true 时设置 Session ,则会出现会话超时问题(如果用户在网站上,但有一段时间没有执行任何操作) 。所以我认为,如果我在丢失全局变量信息之前不久设置会话并在重新初始化后删除会话,那会很好。

这是我的想法,但我不知道这样的事情是否可能。

编辑2: 如果我遵循它的工作原理:

//Global variable
bool test = false;

protected void Page_PreRender(object sender, EventArgs e)
{
    Session["Test"] = test;
}

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        test = (bool)Session["Test"]; // Session is true!!!
        Session.Contents.Remove("Test");
    }
    else
    {
        test = true; // Set at the PageLoad the var to true.
    }
}

我有点困惑,我认为 PreRender 是在 PageLoad 之后,为什么突然测试变量为 true 并且如果我删除 < code>PreRender 不是吗?

格瑞兹

is there an easy way, to store all needed global variables in sessions at once, before the PostBack starts? Or have I to store them in each step where I change them?
I will do something like:

// Global variable.
bool test = true;

// Store all needed information in a session.
protected void Before_globalvariable_is_set_to_default_value(...)
{
    Session["Test"] = test;
    ...
}

protected void Page_Load(object sender, EventArgs e)
{
    if(IsPostBack)
    {
        //if(Session["Test"] != null)
        //{
            test = (bool)Session["Test"];
            Session.Contents.Remove("Test");
        //}
    }
}

Is something like that possible?

Additional Information

At the Page_Load (!IsPostBack) I check if the user gets more vision, if he gets, I set a global var to true. Later in my code I check if that var is true and add additional columns to a GridView.
Now if a PostBack occurs, I can’t check that var, because I lose the information. I knew that I need to store the information in a Session. If I set the Session at the time where I set the global var to true, I get problems with the session timeout (If the user is on the site, but doesn’t do something for a while). So I thought it will be good, if I set the Session shortly before I lose the information of the global var and delete the Session after reinitialization.

That’s my idea, but I don’t know if something like that is possible.

Edit2:
If I do following it works:

//Global variable
bool test = false;

protected void Page_PreRender(object sender, EventArgs e)
{
    Session["Test"] = test;
}

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        test = (bool)Session["Test"]; // Session is true!!!
        Session.Contents.Remove("Test");
    }
    else
    {
        test = true; // Set at the PageLoad the var to true.
    }
}

I’m a little bit confused, I thought PreRender is after the PageLoad, why suddenly the test var is true and if I remove the PreRender it isn’t?

Greetz

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

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

发布评论

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

评论(2

傻比既视感 2024-12-24 17:23:43

如果您担心在请求之间丢失特定值,因为您已经在 Session 对象中维护了该变量的状态,并且它可能已因超时而被清除,那么您可以考虑使用另一个,更持久的保存状态的机制:例如cookies或数据库。

If you're worried about losing a specific value between requests, because you've maintained the state of that variable in the Session object and it might have been cleared by a timeout, you could consider using another, more durable, mechanism to save the state: for example, cookies or database.

南风起 2024-12-24 17:23:43

如果该值仅需要在该一个请求期间存在,则可以使用代码隐藏类的类级字段。在初始化或加载阶段设置它们,然后您可以在所有其他阶段使用这些值。

对于单个请求的生命周期:

public partial class MyPage: Page
{
   private bool test = true;

   public void Page_Load(...)
   {
      // maybe set 'test' to another value
   }

   public void Button_Click(...)
   {
      // you can still access 'test'
   }

   public void Page_PreRender(...)
   {
      // you can still access 'test'
   }
}

但是,如果您需要该值从请求到下一个回发,您可以使用ViewState而不是Session. 优点:没有超时,因为它存储在 html 中并与其他数据一起从浏览器发回。 缺点:它仅在回发扫描中有效,当您链接到其他页面时无效。

If the value only needs to live during that one Request, you can use class-level fields of the code-behind class. Set them in the Init or Load phase, then you can use those values in all other phases.

For a lifetime of just a single request:

public partial class MyPage: Page
{
   private bool test = true;

   public void Page_Load(...)
   {
      // maybe set 'test' to another value
   }

   public void Button_Click(...)
   {
      // you can still access 'test'
   }

   public void Page_PreRender(...)
   {
      // you can still access 'test'
   }
}

If however you need that value to live from request to the next postback, you can use ViewState instead of Session. Advantage: no timeout as it is stored in the html and posted back from the browser along with other data. Disadvantage: it only works in postback-scanario's, not when you link to a different page.

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