Inherit master page?

G

Guest

1. I have a master page. Is it possible to create another master page inherit
the existed one?

2. Is it possible to inject css link, js script block to the head part of
web content page?
 
C

Cowboy \(Gregory A. Beamer\)

Matt said:
1. I have a master page. Is it possible to create another master page
inherit
the existed one?

How much do you love design view? Technically, it is possible, but it does
not render right now.
2. Is it possible to inject css link, js script block to the head part of
web content page?

If you have a content area in the head, you can attach js, etc. to it. You
can also inject into the master header.
 
D

David R. Longnecker

1. I have a master page. Is it possible to create another master page
inherit the existed one?

Using Visual Studio 2005 and ASP.NET 2.0? Not "really". You'll lose the
designer view, but if you're fine with dealing with the code, there's a way.

If you create both of your master pages and then, in an inherited "defaultPage",
you can code it up to pass the master page as a parameter.

private string runtimeMasterPageFile;

public string RuntimeMasterPageFile
{
get { return runtimeMasterPageFile; }
set { runtimeMasterPageFile = value; }
}

protected override void OnPreInit(EventArgs e)
{
if (runtimeMasterPageFile != null)
{
MasterPageFile = runtimeMasterPageFile;
}

base.OnPreInit(e);
}

So, on the page that would inherit both, your Page tag would look like below.
In this example, I have a Default.master page, but also a Secondar.master
page.

<%@ Page Language="C#" CodeFileBaseClass="DefaultPage" MasterPageFile="~/Default.master"
RuntimeMasterPageFile="~/Secondary.master"
AutoEventWireup="true" CodeFile="ViewStudent.aspx.cs"
Inherits="ViewStudent" Title="Untitled Page" Async="true" %>

The code for this was snatched up from Scott Guthrie's blog at http://weblogs.asp.net/scottgu/archive/2005/11/11/430382.aspx
and has worked great over the past couple of years.

Now, if you're looking forward or experimenting with the new Visual Studio
2008 and .NET 3.0/3.5, MasterPages CAN use other MasterPages. The Page tag
is rendered as below. When you create a new master page, it has the option,
just like any other page, to select a master page.

<%@ Master Language="C#" MasterPageFile="~/ParentMasterPage.master" AutoEventWireup="false"
CodeFile="ChildMasterPage.master.cs" Inherits="ChildMasterPage" %>
2. Is it possible to inject css link, js script block to the head part
of web content page?

Explore the Page.Header object and it's attributes. For example, for your
CSS link:

HtmlLink customStyleSheet = new HtmlLink();
customStyleSheet.Href = "StyleSheet.css";
customStyleSheet.Attributes.Add("type", "text/css");
customStyleSheet.Attributes.Add("rel", "stylesheet");
Page.Header.Controls.Add(customStyleSheet);


If nothing else, you can add a <div> with an id and runat=server attribute
and drop controls, or CSS in, with literal controls.

<head runat="server">
<div id="MyCustomContent" runat="server" />
</head>

Then, in code behind:

protected void Page_Load(object sender, EventArgs e)
{
MyCustomContent.InnerHtml = @"<link href='StyleSheet.css' rel='stylesheet'
type='text/css' />";
}

You could do the same with a <script> tag, I'm assuming..

HTH.

-dl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top