Technical Training
ASP.NET 2.0Table of Contents
ASP.NET Changing Master Pages Dynamically
ASP.NET Changing Master Pages Dynamically - Page 2ASP.NET Changing Master Pages Dynamically Page - 2
ASP.NET Changing Master Pages Dynamically
.
.
To add buttons for selecting an alternate master page
1. Open the MasterPage2.master page.
2. In the Toolbox, from the Standard node, drag a LinkButton control onto the page and place it below the layout table.
3. Set the button's Text property to Colorful.
4. Double-click the button to create a handler for its Click event, and then add the following highlighted code:
public partial class MasterPage2_master
5. The code loads the file name of the alternate master page into a persistent session variable, and then reloads the current page. (The Request.URL property returns a URL object that references the current page.) Shortly, you will create code in the content page that will use the name of the master page.
6. Open the MasterPage.master page.
7. Add a LinkButton control as you did in Steps 1 and 2 and set its Text property to Master 1.
8. Double-click the Master 1 button to create a handler for its Click event, and then add the following highlighted code:
9. This code is the same as that for the button in the MasterPage2.master page, except that it loads an alternate master page.
The content page will now dynamically load the master page selected by the user.
To write code to dynamically select the master page
1. Open the About.aspx page.
2. Note that the home page created earlier contains a MasterType directive that binds this content page to the MasterPage.Master and no other master pages can be assigned dynamically to the home page. The developer will have to work with other pages that have been created.
3. In Solution Explorer, right-click About.aspx, and then click View Code to open the code editor.
4. Add the following code:
void Page_PreInit(Object sender, EventArgs e)
{
.....if(Session["masterpage"] != null)
.....{
.....this.MasterPageFile = (String) Session["masterpage"];
.....}
}
5. The code sets the value of the current page's MasterPageFile property to the value in the session variable, if any. This code must run in the Page_PreInit handler; it cannot run in a handler that occurs any later than the Page_PreInit handler (for example, in the Page_Init handler), because the master page must be established so that the page can create an instance of it before any further initialization can occur.
6. The process can now be tested.
To test the dynamic master pages
1. Open the About.aspx page, and then press CTRL+F5 to run it.
2. The page is displayed in the browser merged with its default master page, Master1.master.
3. Click the Master 1 link at the bottom of the page.
4. The page is redisplayed, this time merged with Master2.master, which has a different color schema.
5. Click the Colorful link.
6. The page is displayed using MasterPage.master again.
In this section we have examined how to create multiple master pages and also swtich between the different master pages. In the section that follows we shall briefly look at how to create nested Master pages.
ASP.NET 2.0
- Getting started with ASP.NET 2.0
- .NET Framework Fundamentals
- Microsoft.NET Framework Tools
- Application Development in .NET
- What's New in the .NET Framework 2.0 ?
- Introduction to Visual Studio.NET
- Installing Visual Studio.NET 2005
- Working with Visual Studio.NET Web Applications
- Whats New in ASP.NET 2.0
- Creating an ASP.NET Application
- ASP.NET Code Directory
- ASP.NET Page Object Model
- ASP.NET Server Controls
- ASP.NET Working With Master Pages
- ASP.NET Creating Content for Master Page
- ASP.NET Referencing Master Page Members
- ASP.NET Changing Master Pages Dynamically
- ASP.NET Creating Nested Master Pages
- ASP.NET Working with Web Parts
- ASP.NET Using Web Parts and Controls in Web Pages
- ASP.NET Web Pages and Layout
- ASP.NET - Adding Web Parts at Run Time
- ASP.NET Personalization: User Profiles and Themes
- ASP.NET Data Access features
- ASP.NET State Management
- ASP.NET Customizing the Session State Mechanism
- ASP.NET State Management And Caching
- ASP.NET Security
- Forms Authentication in ASP.NET
- ASP.NET Managing Membership and Roles
- ASP.NET Configuring Page-Level Caching
- ASP.NET Setting Application-Level Caching
- ASP.NET Data Source Object Model
- ASP.NET SqlDataSource Control
- ASP.NET Data Bound Controls
- ASP.NET GridView Control
- ASP.NET GridView Filtering
- ASP.NET Adding Sorting and Paging in GridView
- ASP.NET DataBound Controls - Details View
- ASP.NET Using a Grid to Display Detail Information
- ASP.NET Displaying Master-Detail Data on the Same Page
- Displaying Master-Detail Data on Separate Pages in ASP.NET
- ASP.NET Creating Web Wizards
- ASP.NET : Dynamic Image control
- ASP.NET Advanced Site Functionality







