corymathews
corymathews

Reputation: 12619

Include Content in .aspx masterpage

In my current project we have 5 different masterpages, there are some common elements in each and its really annoying making the change in all 5, it kind of defeats the point of masterpages.

I have tried having parent and child master pages but that caused other problems for a different day.

Is there a way to include dynamic content in a masterpage?

I'm looking for something similar to the php and coldfusion include().

Upvotes: 4

Views: 1707

Answers (5)

Kenneth J
Kenneth J

Reputation: 4896

You could set the masterpages to inherit from a class that dynamically inserts content or script to the page OnPreRender in code. It may seem out there but I have had to use this method.

Upvotes: 0

Joel Etherton
Joel Etherton

Reputation: 37543

As mxmissile and JMP suggested, user controls are the way to go, but you might want to be thorough in your usage of them. When you include the master page, make sure you add the following markup along with the page declaration:

<%@ MasterType 
    virtualpath="~/myMasterPages/Master.master" 
%>

This will allow you to call functions/objects in your master pages so you can make changes to controls or have access from the page itself to various other objects. I have a property in my base usercontrol class called "ParentForm" that is a reference to the page it sits in. For user controls in the master page, I ended up having the same property and in the setter of that property I translate it down to the user controls.

Upvotes: 1

JMP
JMP

Reputation: 7844

You can put user controls (.ASCX) in your master pages. Is this what you were attempting to accomplish?

Like so...

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="WebForms.master.cs"
    Inherits="Tunafish.Web.Views.Shared.WebForms" %>

<%@ Register Src="~/Content/Controls/SiteNavigation.ascx" TagName="Nav" 
    TagPrefix="sc" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <asp:ContentPlaceHolder ID="HeadContent" runat="server" />
</head>
<body>
        <sc:Nav runat="server" />

Upvotes: 4

mxmissile
mxmissile

Reputation: 11681

Have you looked into User Controls? .ascx

Upvotes: 1

Related Questions