Friday 16 February 2007

Setting a Page Theme

Setting a page theme programmatically can be troublesome as the theme can only be set in the Page PreInit. If the theme value is coming from a control on the page then the controls value is not available in the PreInit.
The following solution was taken from : (K.Scott Allen's) OdetoCode.Com

protected void Page_PreInit(object sender, EventArgs e)
{
if (IsPostBack)
{
string uniqueID = Request[_themeListIDKey];

if (uniqueID != null && Request[uniqueID] != null)
{
Theme = Request[uniqueID];
}
}
}

protected void Page_Load(object sender, EventArgs e)
{
ClientScript.RegisterHiddenField(
_themeListIDKey, _themeList.UniqueID
);
}

const string _themeListIDKey = "_themeListIDKey";