How To? Programmatically Set Theme For Master Page

J

Joey

Hello,

I have tried to programmatically set the theme for my master page by
putting code in a "Page_PreInit" fuction in the master page's code
behind. However, when I set a breakpoint there and run the app, the
breakpoint is never hit. Of course my theme is not being applied.

The above procedure works fine for any content page, but obviously
doesn't work for my master page. So, how does one properly and
programmatically set a theme for a master page?
 
C

clintonG

Try using a base class that inherits from System.Web.UI.Page and of course
all content pages then inherit from the base class. Leave the code in the
Master alone. If your using Membership, Roles and Profiles you'll have to
use a cookie and the Profile to store the preferred Theme. Here's some code
from my base class to get started...

protected void Page_PreInit(object sender, EventArgs e)
{

#region Theme Selection...
// This region sets the Theme.
// A cookie is used to persist the selected Theme which reloads the
Theme
// for logged in users who would otherwise have to log in and
reselect
// their preferred Theme. The Theme is also stored in the Profile.
If the
// cookie is not found the preferred Theme is retrieved from the
Profile
// and the cookie is restored. The cookie also enables anonymous
users
// to persist a preferred Theme for a short period of time.

String preferredTheme;

// Get the selected theme from the cookie if the cookie is found.
if (Request.Cookies["selectedTheme"] != null)
{
preferredTheme = Request.Cookies["selectedTheme"].Value;

// Set the Theme.
switch (preferredTheme)
{
case "SmokeyBlues":
this.Theme = "SmokeyBlues";
break;
case "WisconsinAutumn":
this.Theme = "WisconsinAutumn";
break;
default:
this.Theme = "SmokeyBlues";
break;
}
}
else
{
// The cookie was not found so the Theme is retrieved from the
Profile.

// NOTE: Profile information is associated with the current user
based upon the
// identity of the user. By default, ASP.NET uses the
Page.User.Identity.Name
// within the current HttpContext as the key to store data. By
default,
// profiles are available only for authenticated users.

ProfileCommon pc = (ProfileCommon)HttpContext.Current.Profile;

if (pc.UserName != null)
{
preferredTheme = pc.LoggedInUser.SelectedTheme.ToString();
if (preferredTheme != null && preferredTheme.Length > 0)
{
//Set the preferred theme
this.Theme = preferredTheme;
}

if (Request.Cookies["selectedTheme"] == null)
{
// The Theme is only restored from the Profile when the
cookie
// is not found. Since we restored the Theme using the
Profile
// we also have to restore the cookie.
HttpCookie selectedTheme = new
HttpCookie("selectedTheme");
selectedTheme.Value = preferredTheme;
Response.Cookies.Add(selectedTheme);
selectedTheme.Expires = DateTime.Now.AddYears(10);
}
}
}

#endregion

}// Page_PreInit
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top