0

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 from the database.

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

Jeff LaFay
  • 12,882
  • 13
  • 71
  • 101
subs
  • 2,189
  • 12
  • 35
  • 59
  • What kind of data is being shared? Is it user session specific? Or application-level meta data? – Kon Mar 14 '11 at 14:25
  • it is user specific. But i have to fetch the data again from database if the user does some modification on the first usercontrol(it has 2 textboxes). Most of the time the user will postback only after editing these textboxes. In that case i have to fetch the data again from the DB. If I use session I have to replace the session values agin. Thats why i am not sure whether to use session or not. – subs Mar 14 '11 at 14:31
  • One way to update the DB as you're interacting with this variable in your user control(s), would be within the property's setter. – Kon Mar 14 '11 at 15:13

5 Answers5

2

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.

Community
  • 1
  • 1
Kon
  • 27,113
  • 11
  • 60
  • 86
  • If I use a static class, will it not be visible to all the users? – subs Mar 14 '11 at 14:34
  • So if this is a user session-based variable, better to keep it encapsulated inside the user controls and their base class. – Kon Mar 14 '11 at 14:50
1

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.

Thyamine
  • 1,228
  • 7
  • 10
1

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)

Kipotlov
  • 518
  • 3
  • 10
1

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.

Sly
  • 15,046
  • 12
  • 60
  • 89
1

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

Community
  • 1
  • 1
cdonner
  • 37,019
  • 22
  • 105
  • 153