Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [181]

By Root 5632 0
default skin for that control type is used. The following code shows two named skins for a button within the same theme:

When you enable theming on a page, by default all controls in that page will be themed except controls and individual control properties that explicitly disable theming.

Taking Control of Theming


The ASP.NET theming infrastructure provides the EnableTheming Boolean property to disable skins for a control and all its children. You can configure a page or control to ignore themes by setting the EnableTheming property to false. The default value of the property is true. EnableTheming is defined on the Control class and inherited by all server controls and pages. If you want to disable theme support for all controls in a page, you can set the EnableTheming attribute on the @Page directive.

Important

Note that the EnableTheming property can be set only in the Page_PreInit event for static controls—that is, controls defined in the .aspx source. For dynamic controls—that is, controls created programmatically—you must have set the property before adding the control to the page’s control tree. A control is added to the page’s control tree when you add to the Controls collection of the parent control—typically, the form or another control in the form.

When is disabling themes useful? Themes are great at ensuring that all page controls have a consistent look and feel, but at the same time themes override the visual attributes of any control for which a skin is defined. You can control the overriding mechanism a bit by switching style sheet and customization themes. However, when you want a control or page to maintain its predefined look, you just disable themes for that page or control.

Note that disabling themes affects only skins, not CSS styles. When a theme includes one or more CSS style-sheet files, they are linked to the tag of the resulting HTML document and, after that, are handled entirely by the browser. As you can easily guess, there’s not much a Web browser can know about ASP.NET themes!

Loading Themes Dynamically


You can apply themes dynamically, but this requires a bit of care. The ASP.NET runtime loads theme information immediately after the PreInit event fires. When the PreInit event fires, the name of any theme referenced in the @Page directive is already known and will be used unless it is overridden during the event. If you want to enable your users to change themes on the fly, you create a Page_PreInit event handler. The following code shows the code file of a sample page that changes themes dynamically:

public partial class TestThemes : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack) {

// Populate the list of available themes

ThemeList.DataSource = GetAvailableThemes();

ThemeList.DataBind();

}

}

void Page_PreInit(object sender, EventArgs e)

{

string theme = "";

if (Page.Request.Form.Count > 0)

theme = Page.Request["ThemeList"].ToString();

if (theme == "None")

theme = "";

this.Theme = theme;

}

protected StringCollection GetAvailableThemes()

{

var path = Request.PhysicalApplicationPath + @"App_Themes";

var dir = new DirectoryInfo(path);

var themes = new StringCollection();

foreach (var di in dir.GetDirectories())

themes.Add(di.Name);

return themes;

}

}

The drop-down list control named ThemeList enumerates the installed application themes and lets you choose the one to apply. The selected theme is then applied in the PreInit event and immediately reflected. In the PreInit event, no view state has been restored yet; so Request.Form is the only safe way to access a posted value like the selected theme.

Page Usability Checklist


Mastering the technology for building a Web site is necessary, but often it’s not sufficient. Your site must be able to attract people and make them return on a regular basis. A site must

Return Main Page Previous Page Next Page

®Online Book Reader