4

Hi I use Sessions in my application and I was wondering what is the best practice for maintaining session value on a page. I need the value that is stored in the session to be saved until the post back on a particular page. Let me illustrate:

Page A:                             Page B:
Sets Session["ID"] = 5       -->    Gets Session["ID"]
And redirects to Page B             and populates data.

I need the Session["ID"] on Page B to stay alive until a post back is performed on Page B. I need this because I will be using the value of Session["ID"] update data in the database.

My question is: Is the any guarantee that the Session["ID"] will maintain its value until a post back is performed? Won't it die? If so: What are some methods I can take to make sure it maintains its value?

I was thinking of making a private variable in Page B to store the Session["ID"] on Page_Load. Please let me know what you think, thank you.

I'm sorry, I should have said: I cannot allow the user to modify the ID. So no Query Strings, thanks.

4

6 回答 6

2

会话默认存储在服务器的内存中。如果用户的会话超时,这可能会出现问题。此外,如果您将多个服务器用作场,则不能在内存中使用 Session,因为您不能保证用户每次都会使用相同的服务器。

保证会话信息不会丢失的最好方法是将用户的会话持久化到 SqlServer 数据库。这是有关如何设置它的教程。

Session 并不像一些海报所暗示的那样是性能杀手。这是一种非常有用的方法,可以轻松维护每个用户的状态。

另请参阅我对这个问题的回答,了解包装 ASP.NET 会话对象的好方法。

于 2009-03-13T19:22:20.340 回答
1

由于您使用的是 asp.net,并且您希望将数据从一个页面传输到另一个页面,您还可以使用 CrossPage 回发

来自 msdn 的跨页回发:

在某些情况下,您可能希望将一个页面发布到另一页面。例如,您可能正在创建一个多页表单,该表单在每个页面上收集不同的信息。在这种情况下,您可以将页面上的某些控件(实现 IButtonControl 接口的控件,例如 Button 控件)配置为发布到不同的目标页面。这称为跨页发布。与使用 Transfer 方法重定向到另一个页面相比,跨页面发布提供了一些优势。有关详细信息,请参阅将用户重定向到另一个页面。

if (Page.PreviousPage != null)
{
    TextBox SourceTextBox = 
        (TextBox)Page.PreviousPage.FindControl("TextBox1");
    if (SourceTextBox != null)
    {
        Label1.Text = SourceTextBox.Text;
    }
}

来源:http: //msdn.microsoft.com/en-us/library/ms178139.aspx

这是我们在一些项目中使用的方法,并且效果很好。我不知道它是否适合您的需求,但可能值得一看。

于 2009-03-13T19:55:21.947 回答
0

Instead of using session you could transfer the variable from page to page either through the querystring or though a hidden post field, that way it would live with the client and not timeout like it would if it was in the session.

EDIT: If you don't want the user to tamper with the value you could encrypt/obfuscate the hidden post field.

于 2009-03-13T18:17:00.450 回答
0

Session variables stay alive until the session times out (timeout period being specified in your web.config file)

You need to remember persisting data in the session takes up resources on the server so you need to be careful.

In asp.net you can use the viewstate to persist data across postbacks on the same page.

For your question I would recommend using the URL query string parameters to pass the ID between pages

i.e. myurl.aspx?ID=5

then in your aspx code do:

int ID = (int)page.request.params["ID"];

Edit:

If you are concerned with users altering the ID parameter you can put business logic in the pageload of page B to ensure the user has the proper rights to alter that ID value.

于 2009-03-13T18:18:38.620 回答
0

If the session timed out, your value would of course be lost. You could store it in a variable in page load - that should work.

The real thing I'm wondering is why don't you just pass it as a GET parameter when you redirect from Page A to Page B? You might have a perfectly valid reason, but normally if I wanted to pass data from one page to another, that's how I would do it.

于 2009-03-13T18:19:04.500 回答
-1

如果我们有一个不需要会话超时的页面,那么按照

来源:https ://msdn.microsoft.com/en-us/magazine/cc163730.aspx ,

我们可以使用 <%@Page EnableSessionState="False" %>

该链接还讨论了各种最佳实践。

于 2015-03-11T19:23:21.210 回答