469,610 Members | 1,853 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,610 developers. It's quick & easy.

How To? Programmatically Set Theme For Master Page

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?

Apr 17 '06 #1
1 7805
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

"Joey" <jo*********@topscene.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
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?

Apr 17 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by sasquatch | last post: by
1 post views Thread by Miguel Dias Moura | last post: by
reply views Thread by damiensawyer | last post: by
5 posts views Thread by 3Dfelix | last post: by
reply views Thread by Jeff | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.