Go Back   Velocity Reviews > Newsgroups > ASP Net
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

ASP Net - How To? Programmatically Set Theme For Master Page

 
Thread Tools Search this Thread
Old 04-17-2006, 07:06 PM   #1
Default 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?



Joey
  Reply With Quote
Old 04-17-2006, 11:25 PM   #2
clintonG
 
Posts: n/a
Default Re: How To? Programmatically Set Theme For Master Page

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" <> wrote in message
news: 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?
>



  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump