简体   繁体   中英

asp.net multiple non nested masterpages in one project

I have an asp.net site built using visual studio 2010 in c#. The site is basically two sections: http://www.wsgelectronics.com for reference.

The default page is the site.master page with default.aspx. I want to split into two more master pages, one for each section, car audio and computer sections if you look at the site, each with its own layout, nav bar, content etc.

How would I go about linking them to work? Can I have three * .master pages on one project? I didn't choose nested cause they would be completely different layouts, but I cannot find any tutorials on linking different master pages on one site. Can this even be done?

Yes, you can have as many master pages as you need in a given web site. When you create a Web Form (a regular .aspx page) in your site, there's a checkbox for "Select master page", so make sure that's checked. Then, when you pick your desired master page, Studio will stub out the needed markup for your page to use that master page.

For example, here's a stub of a web form that has a master in the same directory, called MasterPage.master . The relevant parts are:

  1. In the Page directive, the property MasterPageFile shows the application relative path to the master page.
  2. Master pages can have many server controls of type ContentPlaceHolder . This allows you to control where the markup in your pages are displayed. The master page I used in this example has two, one is called head and the other is called ContentPlaceHolder1 . Again, those controls are defined in my master page.

Sample markup:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>

For more info, see Master Page Tutorials on asp.net.

Yes you can use multiple master page but at a time one master page should be referenced. You can inherits multiple master page at code behind by specifying the master page in the pre_Init event of page life cycle.

All you need to do is to define a property on the content page which will set the desired Master page in the pre_Init event may be a session variable making it dynamic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM