开发者

Include Content in .aspx masterpage

开发者 https://www.devze.com 2022-12-19 11:29 出处:网络
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.

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 par开发者_运维技巧ent 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().


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" />


Have you looked into User Controls? .ascx


Custom web controls are the way to go:

http://weblogs.asp.net/scottgu/archive/2006/11/26/tip-trick-how-to-register-user-controls-and-custom-controls-in-web-config.aspx


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号