Dynamically setting a theme in ASP.NET Whidbey from a drop down list box
With some suggestions from ScottW and J. Ambrose Little, I just got done with some code to dynamically set a page's theme thru a drop down list box. While this code works, I think the code that I finished with is pretty ugly, so if you have some suggestions, I am more than glad to hear them. BTW, I tried to use the Control.ClientID to get the value from the Request.Form collection with little luck. Even through the form on the master page is set to perform a GET, it still seems to be performing a POST. Note that UglySkin is a theme that I created that is visually disgusting.
Wally
<%@ Page Language="C#" MasterPageFile="~/ExampleMasterPage.master" Title="Theme Skin Page" %>
<script runat="server">
void ddlTheme_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect(Request.ServerVariables["Script_Name"].ToString() + "?ThemeValue=" + Convert.ToString(ddlTheme.SelectedItem.Text));
}
void Page_PreInit(Object sender, System.EventArgs e)
{
if (Request.QueryString["ThemeValue"] != null )
{
string strValue = Request.QueryString["ThemeValue"].ToString();
Page.Theme = strValue;
}
}
void Page_Load(object sender, EventArgs e)
{
if ( ( Request.QueryString["ThemeValue"] != null) && ( !Page.IsPostBack ) )
{
ddlTheme.SelectedIndex = -1;
string strValue = Request.QueryString["ThemeValue"].ToString();
foreach (ListItem liItem in ddlTheme.Items)
{
if (liItem.Value == strValue)
{
liItem.Selected = true;
break;
}
}
}
}
</script>
<asp:Content ID="cObj" ContentPlaceHolderID="cph1" Runat="server">
<asp:DropDownList
ID="ddlTheme" Runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlTheme_SelectedIndexChanged">
<asp:ListItem></asp:ListItem>
<asp:ListItem>BasicBlue</asp:ListItem>
<asp:ListItem>SmokeAndGlass</asp:ListItem>
<asp:ListItem>UglySkin</asp:ListItem>
</asp:DropDownList><br />
<asp:Label ID="lblText" Runat="Server">Theme Test.</asp:Label><br />
<asp:TextBox ID="txtText" Runat="server">Text Box Test</asp:TextBox><br />
</asp:Content>