alt
Advertisement
Online Training
Career Series
Exforsys
Exforsys arrow Tutorials arrow ASP.NET 2.0 arrow ASP.NET 2.0 :Changing Master Pages Dynamically
Site Search


ASP.NET 2.0 :Changing Master Pages Dynamically
Article Index
ASP.NET 2.0 :Changing Master Pages Dynamically
Page 2

.

.

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.


Trackback(0)
Comments (1)add comment

Megan McVey said:

  8. Double-click the Master 1 button to create a handler for its Click event, and then add the following highlighted code:

(and there is no code)

I mean, ya, I can figure it out (and want to store in the DB anyway not the session...) but still!!

Megan
March 15, 2007

Write comment

busy

 
< Prev   Next >
Sponsored Links
© 2008 Exforsys.com
Joomla! is Free Software released under the GNU/GPL License.
Page copy protected against web site content infringement by Copyscape