Reputation: 1669
This is basicly the design question, Like what would be the best way to structure my code within my web asp.net 4.0 web forms application. My General Design is like this...
Master Page
Has few User Controls
Content Pages
Inherit from Base Class
User Controls within Content Pages
I need to get some setting stuff from database in one CALL & store results into a Class. and then this class will be shared throughout the Pages (in Master Page, Base Class, Content Pages and user controls).
What would be the best place to initialize this class and easily access it throughout the page lifectycle.
Should I Hit Database & initialize the Class in BASE PAGE and then update Master Pages from within the BASE Class..like this
Page.master.findControl("label").Text = myClass.Setting1
and then Content Pages and its user control would need access to this class as well.
Please advise the best practice.
Upvotes: 0
Views: 114
Reputation: 15893
If this is completely shared data common for all users of your application, put it in a data container exposed as a static property of a class, and fill this container with data from database in the static constructor of this class.
public class SettingsData
{
public string Setting1;
public string Setting2;
}
public static class DataHolder
{
public static SettingsData Settings = new SettingsData();
static DataHolder()
{
// open connection to db and query for values
Settings.Setting1 = <value from db>;
Settings.Setting2 = <value from db>;
}
}
then in code
Label1.Text = DataHolder.Settings.Setting1;
Upvotes: 1