Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

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

RSS
Modified on Thu, Dec 22, 2011, 1:48 PM by Administrator Paths: MCTS Exam 70-515 Categorized as 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

  • Skins — *.skin files
  • Cascading Style Sheets — *.css files
  • Images and other resources

Creation

  • Each theme has its own subfolder under the ~/App_Themes folder.
  • The name of the theme matches the subfolder name.

Application

  • Page level — Done in the @Page directive
  • Web.config level — Done via the <page Theme="ThemeName" > or <page StyleSheetTheme="ThemeName" > in the <system.web> section.
  • Properties defined via StyleSheetTheme are applied before a page's control property set, and therefore can be overridden. By contrast, properties defined via Theme are applied after a page's control property set, and therefore cannot be overridden.

Creating a Skin

Types of Skins

  • Default Skins — Has no SkinID attribute; automatically applies to all controls of the EXACT same type when a theme is applied to a page.
  • Named Skins — Has a SkinID attribute; applied manually to each control via its SkinID attribute.

Restrictions

  • Skin elements MUST contain the runat="server" attribute.
  • Skin elements CANNOT contain the ID attribute.

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

  • For a page: <%@ Page EnableTheming="false" %>
  • For a control: EnableTheming="false"

Applying a Theme Programmatically

Apply any of the following in the Page_PreInit event handler.

  • Page.Theme
  • Page.StyleSheetTheme
  • uxControl.SkinID

Lesson 3: Caching

Application Caching

Overview

  • Use the Cache object just like Session or similar objects: Cache["key"] = object;
  • Cache items are application-wide: shared between users and requests
  • Objects placed into the cache must be serializable

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.

  • SetExpires — specifies the number of seconds the page is cached.
  • SetCacheability — specifies HttpCacheability enum: Public (caching at both client and server) or Server (caching at server only).
  • SetValidUntilExpires — pass a True to configure the cache to ignore cache-invalidation headers.

Using Substitution to Update Caches

  • Response.WriteSubstitution method — You add static placeholders to your page, and then use the Response.WriteSubstitution method to specify a method (with an HttpResponseSubstitutionCallback signature) that replaces portions of a cached page with dynamically generated content.
  • Substitution control — Similar to Label control, but exempt from output caching. The MethodName property specifies the method that generates the content, accepting an HttpContext parameter and returning a String containing the content.

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





ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2018, Patrick Jasinski.