Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

ASP.NET Changing Master Pages Dynamically

By Exforsys | on August 18, 2005 |
ASP.NET 2.0

ASP.NET Changing Master Pages Dynamically

In this tutorial you will learn how to make a copy of the master page, To add buttons for selecting an alternate master page, To write code to dynamically select the master page and test the dynamic master pages.

Changing Master Pages Dynamically

Dynamically changing master pages is possible. The code can be used to set the master for the content. This may especially be useful when users want to select from several different layouts to set their preferences. To see how this is done let us add another Master page to our website and allow the user to select the template he wants.

The first step is to make a copy of the Master as we want the second page to approximate to the first Master.

To make a copy of the master page

1. In Solution Explorer, right-click MasterPag.master, and then click Copy.

2. Right-click the name of the Web site, and then click Paste.

3. A master page is added to the Web site with the name Copy of masterPage.master.

4. Right-click Copy of masterPage.master, click Rename, and then name the new master page MasterPage2.master.

5. Open Master2.master and in the @ Master directive, change MasterPage to MasterPage2.

6. Switch to Design view.

7. In the Properties window, in the drop-down list at the top, click DOCUMENT.

8. Set the BgColor property to the color of your choice.

9. The new master page will look and function like MasterPage.master, but will have a different background color

10. Open the master page’s code file, and then change the class name from MasterPage_master to MasterPage2_master.

The next step is to add a button to each master page that allows the user to select the alternate master page.

.

.

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 Working With Master Pages
ASP.NET Creating Nested Master Pages » »

Author Description

Avatar

Editorial Team at Exforsys is a team of IT Consulting and Training team led by Chandra Vennapoosa.

Free Training

RSSSubscribe 401 Followers
  • Popular
  • Recent
  • ASP.NET Using Web Parts and Controls in Web Pages

    August 22, 2005 - 0 Comment
  • ASP.NET Configuring Page-Level Caching

    August 30, 2005 - 0 Comment
  • ASP.NET Server Controls

    August 15, 2005 - 0 Comment
  • Displaying Master-Detail Data on Separate Pages in ASP.NET

    September 16, 2005 - 0 Comment
  • ASP.NET Web Pages and Layout

    August 23, 2005 - 0 Comment
  • ASP.NET Setting Application-Level Caching

    August 30, 2005 - 0 Comment
  • ASP.NET Working With Master Pages

    August 17, 2005 - 0 Comment
  • ASP.NET Creating Web Wizards

    September 16, 2005 - 0 Comment
  • ASP.NET – Adding Web Parts at Run Time

    August 22, 2005 - 0 Comment
  • ASP.NET Data Source Object Model

    September 6, 2005 - 0 Comment
  • Application Development in .NET

    November 21, 2007 - 0 Comment
  • ASP.NET Advanced Site Functionality

    September 16, 2005 - 0 Comment
  • ASP.NET : Dynamic Image control

    September 16, 2005 - 0 Comment
  • ASP.NET Creating Web Wizards

    September 16, 2005 - 0 Comment
  • Displaying Master-Detail Data on Separate Pages in ASP.NET

    September 16, 2005 - 0 Comment
  • ASP.NET Displaying Master-Detail Data on the Same Page

    September 13, 2005 - 0 Comment
  • ASP.NET DataBound Controls – Details View

    September 13, 2005 - 0 Comment
  • ASP.NET Using a Grid to Display Detail Information

    September 13, 2005 - 0 Comment
  • ASP.NET Adding Sorting and Paging in GridView

    September 10, 2005 - 0 Comment
  • ASP.NET GridView Filtering

    September 10, 2005 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • Application Development in .NET
  • ASP.NET Advanced Site Functionality
  • ASP.NET : Dynamic Image control
  • ASP.NET Creating Web Wizards
  • Displaying Master-Detail Data on Separate Pages in ASP.NET

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2022. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.Accept Reject Read More
Privacy & Cookies Policy
Necessary Always Enabled