Online Book Reader

Home Category

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

By Root 5656 0
of the .NET Framework 4. The net effect is that if at run time the actual object behind the master variable can successfully resolve a call to the specified property, the code works as expected; otherwise, an exception would be raised.

If you’re not using .NET 4, however, you have another choice.

The @MasterType Directive


By adding the @MasterType directive in the content page, you can avoid all the casting just shown. The @MasterType informs the compiler about the real type of the Master property. The Master property is declared as the right type in the dynamically created page class, and this allows you to write strongly typed code, as follows:

<%@ Page Title="Nested master pages"

MasterPageFile="~/MainContent.Master"

AutoEventWireup="true"

CodeBehind="NestedDemo.aspx.cs"

Inherits="Masters.NestedDemo" %>

<%@ MasterType VirtualPath="~/MainContent.Master" %>

In the code file, you can have the following statements:

protected void Page_Load(object sender, EventArgs e)

{

Master.MainContentTitle = "Nested demo";

}

The @MasterType directive supports two mutually exclusive attributes: VirtualPath and TypeName. Both serve to identify the master class to use. The former does it by URL; the latter by type name. Figure 8-8 shows the effect of the directive on the code being created and the nice work Visual Studio IntelliSense does around it.

Figure 8-8. Effect of the @MasterType directive.

Changing the Master Page Dynamically


To associate an ASP.NET content page with a master page—keeping in mind that in no case can you associate a classic ASP.NET page with a master—you use the MasterPageFile attribute of the @Page directive. MasterPageFile, though, is also a read-write property on the Page class that points to the name of the master page file. Can you dynamically select the master page via code and based on run-time conditions?

Using a dynamically changing master page is definitely possible in ASP.NET and is suitable, for example, for applications that can present themselves to users through different skins. However, programmatically selecting the master page is not a task that you can accomplish at any time. To be precise, you can set the MasterPageFile property only during the PreInit page event—that is, before the run time begins working on the request.

protected void Page_PreInit(object sender, EventArgs e)

{

MasterPageFile = "another.master";

}

If you try to set the MasterPageFile property in Init or Load event handlers, an exception is raised.

Note

The Master property represents the current instance of the master page object, is a read-only property, and can’t be set programmatically. The Master property is set by the run time after loading the content of the file referenced by the MasterPageFile property.

Styling ASP.NET Pages


ASP.NET pages are mostly made of server controls, and server controls ultimately serve up HTML markup. The client browser builds and displays HTML elements whose appearance depends on the information stored in their style containers.

It turns out that there are several places for you to add style information to control the look and feel of the page. If you feel comfortable with server controls, you use ASP.NET themes. If you need to exercise more control over the content actually sent to (and displayed by) the browser, you can configure controls to emit CSS-friendly markup that can be styled effectively from the client or through HTML literals right in the ASPX markup.

ASP.NET themes have been introduced with the intent of facilitating the task of styling server controls with the same approach used by cascading style sheets at the HTML element level. Themes were originally aimed at doing the same job of CSS but through a more specific interface tailor-made for server controls. Through themes, you just declaratively define some code to be run to dress the control in a given way. Basically, themes are a way to adapt the CSS syntax to the syntax of server controls.

Boldly introduced as a way to supersede CSS styles, today ASP.NET themes are in a downturn,

Return Main Page Previous Page Next Page

®Online Book Reader