Chapter 02: Using Master Pages, Themes, and Caching - MCTS Exam 70-515


Lesson 1: Using Master Pages

Master Page

<%@ Master
    ...
%>

Content Page

<%@ Page 
    MasterPageFile="~/MyMasterPage.master" 
    ... 
%>
<!-- Option declaration needed if referencing public members of master page from content page -->
<%@ MasterType VirtualPath="~/MyMasterPage.master" %>

Programmatically Changing a Master Page

void Page_PreInit(object sender, EventArgs e)
{
    MasterPageFile = "~/MyMasterPage.master";
}

Lesson 2: Using Themes

Contents


Creation


Application


Creating a Skin

Types of Skins


Restrictions


Sample Skin Content

<!--===== Default Skin =====-->
<asp:Button runat="server"
    BackColor="Red"
    ForeColor="White"
    Font-Name="Arial"
    Font-Size="9px" 
    />

<!--===== Named Skin =====-->
<asp:Label runat="server"
    SkinId="Title"
    Font-Size="18px" 
    />

<!--===== Using Images in a Theme =====-->
<!-- Company A’s Skin File -->
<asp:Image runat="server" SkinID="CompanyLogo"
    ImageUrl="~/App_Themes/Contoso/contoso.jpg" 
    />

<!-- Company B’s Skin File -->
<asp:Image runat="server" SkinID="CompanyLogo"
    ImageUrl="~/App_Themes/Fabrikam/fabrikam.jpg" 
    />

The above image skin would be applied to the following image on a page. When the theme is change, the image would change too.

<asp:Image runat="server" SkinID="CompanyLogo" />

Precedence Rules for Applying Themes

Attributes and elements take precedence in the following order. (Each item takes precedence over preceding items.)

  1. <page StyleSheetTheme="ThemeName" > elements in the <system.web> section of the web.config file.
  2. StyleSheetTheme attributes in the @Page directive.
  3. Local control attributes
  4. <page Theme="ThemeName" > elements in the <system.web> section of the web.config file.
  5. Theme attributes in the @Page directive.

Disabling a Theme


Applying a Theme Programmatically

Apply any of the following in the Page_PreInit event handler.


Lesson 3: Caching

Application Caching

Overview


Inserting Items

Insert items via the Cache.Insert() method, which has overloads using the following parameters.

ParameterDescription
keyUnique name (String) identifying the cached object
valueThe cached data (Object)
dependenciesA file or key to another item, which, when changed, triggers this item to be evicted. Can be a single item (CacheDependency object) or multiple items (AggregateCacheDependency object).
absoluteExpirationA DateTime when the object should be evicted
slidingExpirationDefines the maximum period of inactivity (TimeSpan) before the object is evicted.
priorityA CacheItemPriority indicating which objects are evicted when memory runs low. (Lower priority items are evicted sooner.) Can be Low, BelowNormal, Normal (default), AboveNormal, High, or NotRemovable
onRemoveCallbackAn event handler that's called upon object eviction.

Page Output Caching

Declarative Configuration

Each page can be cached independently via the <%@ OutputCache %> directive, which has the following attributes.

AttributeDescription
DurationThe only required attribute. Number of seconds to cache the page
Location3OutputCacheLocation enum: Any (default), Client, Downstream, Server, None, or ServerAndClient.
CacheProfile3Name of the cache setting to associate with the page. Default="".
NoStore3Boolean that determines whether sensitive data is saved in secondary storage.
Shared4Boolean that determines whether user control output can be shared among multiple pages.
VaryByParam1Semicolon-separated list of strings which correspond to parameter names (query string or POST parameters) by which the output cache should be versioned. Values include none or an asterisk (which varies by all parameters).
VaryByControl1Semi-colon separated list of strings representing IDs of server controls in the user control.
SqlDependency2String that identifies a set of database and table name pairs on which a page's output cache depends.
VaryByCustomAny text representing custom caching. If this is a browser, cache is varied by browser and major version. If this is a custom string, you must override the GetVaryByCustomString method in Global.asax.
VaryByHeaderA semicolon-separated list of HTTP headers to version the output cache by.

1 – Either VaryByParam or VaryByControl must be specified.
2 – The SqlCacheDependency class monitors the database table so when items are update, they're removed from the cache when table-based polling is used. When SQL Server notifications are used with the value CommandNotification, a SqlDependency class is used to register for query notifications with the SQL Server instance.
3 – Cannot be used in a user control.
4 – Cannot be used in an ASP.NET page.

Partial-Page Caching

To cache a portion of a page, move that portion into a user control and use the <@ OutputCache > directive within it. Or you can use Substitution (see below).

Programmatic Caching

Programmatic caching is done via the Response.Cache object, which has the following methods.


Using Substitution to Update Caches


Programmatically Invalidating Cached Pages

Determining Whether to Return a Cached Page Prior to Rendering

Creating a Cache Page Output Dependency

Configuring Caching for an Entire Application