在ASP.NET MVC RC中使用动态母版页面,可以通过以下步骤实现:
DynamicMasterPage.master
。<!DOCTYPE html>
<html>
<head>
<title>Dynamic Master Page</title>
</head>
<body>
<div id="header">
<asp:ContentPlaceHolder ID="HeaderContent" runat="server">
</asp:ContentPlaceHolder>
</div>
<div id="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
</div>
<div id="footer">
<asp:ContentPlaceHolder ID="FooterContent" runat="server">
</asp:ContentPlaceHolder>
</div>
</body>
</html>
<%@ Page Language="C#" MasterPageFile="~/DynamicMasterPage.master" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="Index" Title="Index" %>
<asp:Content ID="HeaderContent" ContentPlaceHolderID="HeaderContent" runat="server">
<h1>Header Content</h1>
</asp:Content>
<asp:Content ID="MainContent" ContentPlaceHolderID="MainContent" runat="server">
<h1>Main Content</h1>
</asp:Content>
<asp:Content ID="FooterContent" ContentPlaceHolderID="FooterContent" runat="server">
<h1>Footer Content</h1>
</asp:Content>
using System;
using System.Web.Mvc;
public partial class Index : ViewPage
{
protected void Page_Load(object sender, EventArgs e)
{
// Add dynamic content to the master page
ContentPlaceHolder headerContent = (ContentPlaceHolder)Master.FindControl("HeaderContent");
ContentPlaceHolder mainContent = (ContentPlaceHolder)Master.FindControl("MainContent");
ContentPlaceHolder footerContent = (ContentPlaceHolder)Master.FindControl("FooterContent");
headerContent.Controls.Add(new LiteralControl("<h1>Dynamic Header Content</h1>"));
mainContent.Controls.Add(new LiteralControl("<h1>Dynamic Main Content</h1>"));
footerContent.Controls.Add(new LiteralControl("<h1>Dynamic Footer Content</h1>"));
}
}
通过以上步骤,可以在ASP.NET MVC RC中使用动态母版页面。
领取专属 10元无门槛券
手把手带您无忧上云