Exforsys

ASP.NET 2.0

  1. Getting started with ASP.NET 2.0
  2. .NET Framework Fundamentals
  3. Microsoft.NET Framework Tools
  4. Application Development in .NET
  5. What's New in the .NET Framework 2.0 ?
  6. Introduction to Visual Studio.NET
  7. Installing Visual Studio.NET 2005
  8. Working with Visual Studio.NET Web Applications
  9. Whats New in ASP.NET 2.0
  10. Creating an ASP.NET Application
  11. ASP.NET Code Directory
  12. ASP.NET Page Object Model
  13. ASP.NET Server Controls
  14. ASP.NET Working With Master Pages
  15. ASP.NET Creating Content for Master Page
  16. ASP.NET Referencing Master Page Members
  17. ASP.NET Changing Master Pages Dynamically
  18. ASP.NET Creating Nested Master Pages
  19. ASP.NET Working with Web Parts
  20. ASP.NET Using Web Parts and Controls in Web Pages
  21. ASP.NET Web Pages and Layout
  22. ASP.NET - Adding Web Parts at Run Time
  23. ASP.NET Personalization: User Profiles and Themes
  24. ASP.NET Data Access features
  25. ASP.NET State Management
  26. ASP.NET Customizing the Session State Mechanism
  27. ASP.NET State Management And Caching
  28. ASP.NET Security
  29. Forms Authentication in ASP.NET
  30. ASP.NET Managing Membership and Roles
  31. ASP.NET Configuring Page-Level Caching
  32. ASP.NET Setting Application-Level Caching
  33. ASP.NET Data Source Object Model
  34. ASP.NET SqlDataSource Control
  35. ASP.NET Data Bound Controls
  36. ASP.NET GridView Control
  37. ASP.NET GridView Filtering
  38. ASP.NET Adding Sorting and Paging in GridView
  39. ASP.NET DataBound Controls - Details View
  40. ASP.NET Using a Grid to Display Detail Information
  41. ASP.NET Displaying Master-Detail Data on the Same Page
  42. Displaying Master-Detail Data on Separate Pages in ASP.NET
  43. ASP.NET Creating Web Wizards
  44. ASP.NET : Dynamic Image control
  45. ASP.NET Advanced Site Functionality

Ads


Home arrow Technical Training arrow ASP.NET 2.0

ASP.NET Changing Master Pages Dynamically Page - 2

Page 2 of 2
Author : Exforsys Inc.     Published on: 18th Aug 2005

ASP.NET Changing Master Pages Dynamically

.

.

Ads

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.



 
This tutorial is part of a ASP.NET 2.0 tutorial series. Read it from the beginning and learn yourself.

ASP.NET 2.0

  1. Getting started with ASP.NET 2.0
  2. .NET Framework Fundamentals
  3. Microsoft.NET Framework Tools
  4. Application Development in .NET
  5. What's New in the .NET Framework 2.0 ?
  6. Introduction to Visual Studio.NET
  7. Installing Visual Studio.NET 2005
  8. Working with Visual Studio.NET Web Applications
  9. Whats New in ASP.NET 2.0
  10. Creating an ASP.NET Application
  11. ASP.NET Code Directory
  12. ASP.NET Page Object Model
  13. ASP.NET Server Controls
  14. ASP.NET Working With Master Pages
  15. ASP.NET Creating Content for Master Page
  16. ASP.NET Referencing Master Page Members
  17. ASP.NET Changing Master Pages Dynamically
  18. ASP.NET Creating Nested Master Pages
  19. ASP.NET Working with Web Parts
  20. ASP.NET Using Web Parts and Controls in Web Pages
  21. ASP.NET Web Pages and Layout
  22. ASP.NET - Adding Web Parts at Run Time
  23. ASP.NET Personalization: User Profiles and Themes
  24. ASP.NET Data Access features
  25. ASP.NET State Management
  26. ASP.NET Customizing the Session State Mechanism
  27. ASP.NET State Management And Caching
  28. ASP.NET Security
  29. Forms Authentication in ASP.NET
  30. ASP.NET Managing Membership and Roles
  31. ASP.NET Configuring Page-Level Caching
  32. ASP.NET Setting Application-Level Caching
  33. ASP.NET Data Source Object Model
  34. ASP.NET SqlDataSource Control
  35. ASP.NET Data Bound Controls
  36. ASP.NET GridView Control
  37. ASP.NET GridView Filtering
  38. ASP.NET Adding Sorting and Paging in GridView
  39. ASP.NET DataBound Controls - Details View
  40. ASP.NET Using a Grid to Display Detail Information
  41. ASP.NET Displaying Master-Detail Data on the Same Page
  42. Displaying Master-Detail Data on Separate Pages in ASP.NET
  43. ASP.NET Creating Web Wizards
  44. ASP.NET : Dynamic Image control
  45. ASP.NET Advanced Site Functionality
 

Comments