Store web.config value in SQL table

J

jack

I have an application wide configuration setting (pages theme) that
is
dynamic. Normally when you think of picking themes you think of the
user selecting a theme (a per-user setting). In this case, though, I
want the site owner to be able to select a theme that will be applied
to the entire site (not per user). I do not want to programmatically
write out/edit the web.config file because of permissions issues. I
simply want the user to be able to set a value in a SQL table that
will then be grabbed (possibly in the Application_OnStart function in
global.asax) that will apply the theme to the entire site. This
isn't
something that would change often, but it has to an editable value
that is not static (like most things in web.config).

I was thinking there might be a way to programmitcally change the
<pages theme=""> setting in web.config in Application_OnStart. Or is
there a way to bypass web.config entirely. I really don't want to
have to add code to every single Page_PreInit event)...


Thanks much.


Jack
 
G

Guest

Hi Jack,

There are many different approaches to this (base class or master page), but
the easiest is to handle one application event in global.asax

void Application_Start(object sender, EventArgs e)
{
// retrieve the value from database;
Application[ThemeKey] = "red";
}

private const string ThemeKey = "DefaultTheme";

protected void Application_PreRequestHandlerExecute(
object sender, EventArgs e)
{
// this is the first event in which
// request handler is instantiated and accessible
Page page = HttpContext.Current.CurrentHandler as Page;
if (page != null)
{
page.Theme = Application[ThemeKey];
}
}

hope this helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top