开发者

Where to store common data to share between pages

开发者 https://www.devze.com 2023-02-16 13:51 出处:网络
I have a page which has 3 user controls. I want some data from database to be shared between these user controls. Where actually i should save it. I am using a static class to store it now. I dont thi

I have a page which has 3 user controls. I want some data from database to be shared between these user controls. Where actually i should save it. I am using a static class to store it now. I dont think this is a good idea.

these data actually depends upon the first usercontrol. So if the user has made any changes to the first usercontrol's controls than i am fetching these data again 开发者_如何学JAVAfrom the database.

I am not sure where should i store this data. Should I use session or a static class?


One way would be to store it in a Session variable and read/write that Session variable via a property defined in a base class that all 3 of your controls inherit from (or, as you said, a static class they all have access to).

Check out this answer for an example of how I would define a property that does read/write on a Session variable.


If it's data that you want to share for a longer time, then saving it into the database and calling it from relevant pages is good. Especially if they may decide to leave/quit before getting to the last page that would otherwise save it.

If you are just saving it between pages, and after that it doesn't need to be kept, using the Session is certainly valid.


This 2 options don't have the same scope.

Session keep data for a user as the user navigates ASP.NET pages in a Web application.

and

Static class keep data for all the live of your Web Application (any users, any sessions)


ASP.NET provides different state management approaches depending on your requirements.

Static class is a bad idea because all your visitors will see the same data.

You can get additional information about states here at pick one that is best for your task.


Your options are:

  • Viewstate [scope is the page, data survives post-back]
  • Session [scope is the client session, data survives across pages]
  • Applicationstate [for global scope]
  • Database and a combination of the above [scope is across client sessions]

Static classes are not a good idea, for reasons already mentioned.

See Maintain state of an asp.net page

0

精彩评论

暂无评论...
验证码 换一张
取 消