Quote:
Originally Posted by Imar
Hi again,
For this code to work, you need three pieces of information:
1. A default Theme in the <pages /> element in web.config; not a styleSheetTheme
2. Code in the BasePage class that sets the selected theme based on a cookie
3. Code in the Master Page's Code Behind to set the cookie and redirect (in lstPreferredTheme) and preselect the right item (in Pre_Init).
Do you have all that?
Imar
|
1. Default theme in the <pages/> element in web.config <pages theme="Monochrome">
====================================
2. Code in the BasePage is:
====================================
using System;
using System.Web;
public class BasePage : System.Web.UI.Page
{
private void Page_PreRender(object sender, EventArgs e)
{
if (this.Title == "Untitled Page")
{
throw new Exception("Page title cannot be \"Untitled Page\".");
}
}
private void Page_PreInit(object sender, EventArgs e)
{
HttpCookie preferredTheme = Request.Cookies.Get("PreferredTheme");
if (preferredTheme != null)
{
Page.Theme = preferredTheme.Value;
}
}
public BasePage()
{
this.PreRender += new EventHandler(Page_PreRender);
this.PreInit += new EventHandler(Page_PreInit);
}
}
==================================
3. This is the code snippet in the master page
==================================
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string selectedTheme = Page.Theme;
HttpCookie preferredTheme = Request.Cookies.Get("PreferredTheme");
if (preferredTheme != null)
{
selectedTheme = preferredTheme.Value;
}
if (lstPreferredTheme.Items.FindByValue(selectedTheme ) != null)
{
lstPreferredTheme.Items.FindByValue(selectedTheme) .Selected = true;
}
}
}
protected void lstPreferredTheme_SelectedIndexChanged(object sender, EventArgs e)
{
HttpCookie preferredTheme = new HttpCookie("PreferredTheme");
preferredTheme.Expires = DateTime.Now.AddMonths(3);
preferredTheme.Value = lstPreferredTheme.SelectedValue;
Response.Cookies.Add(preferredTheme);
Response.Redirect(Request.Url.ToString());
}
}
===========================