Example E1342
Visible to All Users

ASP.NET Web Forms - How to change themes on the fly

This example illustrates how to change themes on the fly.

Implementation Details

You can apply a theme to DevExpress ASP.NET Web Forms controls in the following ways:

Apply a Theme with the ASP.NET Mechanism

Specify a page's Page.Theme property in the Page.PreInit event handler.

C#
protected void Page_PreInit(object sender, EventArgs e) { HttpCookie c = Request.Cookies["theme"]; Page.Theme = c == null ? "Aqua" : c.Value; }

Apply a Theme with the DevExpress Mechanism

Set the ASPxWebControl.GlobalTheme property to the theme name in the Page.PreInit event handler.

C#
protected void Page_PreInit(object sender, EventArgs e) { HttpCookie c = Request.Cookies["theme"]; ASPxWebControl.GlobalTheme = c == null ? "Aqua" : c.Value; }

Files to Review

Documentation

More Examples

Example Code

Default2.aspx
ASPx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <%@ Register Assembly="DevExpress.Web.v13.2, Version=13.2.15.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web" TagPrefix="dx" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>How to change a page's theme dynamically</title> </head> <body> <form id="form1" runat="server"> <table> <tr> <td> <dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" DataSourceID="dataSource" KeyFieldName="CategoryID"> <Columns> <dx:GridViewDataTextColumn FieldName="CategoryID" ReadOnly="True" VisibleIndex="0"> <EditFormSettings Visible="False" /> </dx:GridViewDataTextColumn> <dx:GridViewDataTextColumn FieldName="CategoryName" VisibleIndex="1"> </dx:GridViewDataTextColumn> <dx:GridViewDataTextColumn FieldName="Description" VisibleIndex="2"> </dx:GridViewDataTextColumn> </Columns> </dx:ASPxGridView> &nbsp;<br /> <dx:ASPxTextBox ID="ASPxTextBox1" runat="server" Width="170px"> </dx:ASPxTextBox> <dx:ASPxButton ID="button" runat="server" Text="Button"> </dx:ASPxButton> </td> <td valign="top"> <dx:ASPxRadioButtonList ID="rbList" runat="server" AutoPostBack="True" SelectedIndex="0"> <ClientSideEvents SelectedIndexChanged="function(s, e) { ASPxClientUtils.SetCookie('theme', s.GetValue());}" /> <Items> <dx:ListEditItem Selected="True" Text="Aqua" Value="Aqua" /> <dx:ListEditItem Text="DevEx" Value="DevEx" /> <dx:ListEditItem Text="SoftOrange" Value="SoftOrange" /> <dx:ListEditItem Text="Youthful" Value="Youthful" /> </Items> </dx:ASPxRadioButtonList> <asp:SqlDataSource ID="dataSource" runat="server" ConnectionString="Data Source=(local);Initial Catalog=Northwind;Integrated Security=True" ProviderName="System.Data.SqlClient" SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]"> </asp:SqlDataSource> </td> </tr> </table> </form> </body> </html>
Default2.aspx.cs(vb)
C#
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default2 : System.Web.UI.Page { protected void Page_PreInit(object sender, EventArgs e) { HttpCookie c = Request.Cookies["theme"]; Page.Theme = c == null ? "Aqua" : c.Value; } protected void Page_Load(object sender, EventArgs e) { HttpCookie c = Request.Cookies["theme"]; if(!IsPostBack && (c != null)) rbList.Value = c.Value; } }
Default3.aspx
ASPx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %> <%@ Register Assembly="DevExpress.Web.v13.2, Version=13.2.15.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web" TagPrefix="dx" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>How to change a page's theme dynamically</title> </head> <body> <form id="form1" runat="server"> <table> <tr> <td> <dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" DataSourceID="dataSource" KeyFieldName="CategoryID"> <Columns> <dx:GridViewDataTextColumn FieldName="CategoryID" ReadOnly="True" VisibleIndex="0"> <EditFormSettings Visible="False" /> </dx:GridViewDataTextColumn> <dx:GridViewDataTextColumn FieldName="CategoryName" VisibleIndex="1"> </dx:GridViewDataTextColumn> <dx:GridViewDataTextColumn FieldName="Description" VisibleIndex="2"> </dx:GridViewDataTextColumn> </Columns> </dx:ASPxGridView> &nbsp;<br /> <dx:ASPxTextBox ID="ASPxTextBox1" runat="server" Width="170px"> </dx:ASPxTextBox> <dx:ASPxButton ID="button" runat="server" Text="Button"> </dx:ASPxButton> </td> <td valign="top"> <dx:ASPxRadioButtonList ID="rbList" runat="server" AutoPostBack="True" SelectedIndex="0"> <ClientSideEvents SelectedIndexChanged="function(s, e) { ASPxClientUtils.SetCookie('theme', s.GetValue());}" /> <Items> <dx:ListEditItem Selected="True" Text="Aqua" Value="Aqua" /> <dx:ListEditItem Text="DevEx" Value="DevEx" /> <dx:ListEditItem Text="SoftOrange" Value="SoftOrange" /> <dx:ListEditItem Text="Youthful" Value="Youthful" /> </Items> </dx:ASPxRadioButtonList> <asp:SqlDataSource ID="dataSource" runat="server" ConnectionString="Data Source=(local);Initial Catalog=Northwind;Integrated Security=True" ProviderName="System.Data.SqlClient" SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]"> </asp:SqlDataSource> </td> </tr> </table> </form> </body> </html>
Default3.aspx.cs(vb)
C#
using DevExpress.Web; using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default3 : System.Web.UI.Page { protected void Page_PreInit(object sender, EventArgs e) { HttpCookie c = Request.Cookies["theme"]; ASPxWebControl.GlobalTheme = c == null ? "Aqua" : c.Value; } protected void Page_Load(object sender, EventArgs e) { HttpCookie c = Request.Cookies["theme"]; if(!IsPostBack && (c != null)) rbList.Value = c.Value; } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.