1. 母版页(Site.master):
<!-- Site.master -->
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="WebApplication.Site" %>
<!DOCTYPE html>
<html lang="en">
<head runat="server">
<meta charset="utf-8" />
<title>My Web Forms Site</title>
<asp:ContentPlaceHolder ID="head" runat="server"></asp:ContentPlaceHolder>
</head>
<body>
<form runat="server">
<header>
<h1>My Web Forms Site</h1>
<nav>
<asp:Menu ID="Menu1" runat="server" DataSourceID="SiteMapDataSource1" Orientation="Horizontal">
</asp:Menu>
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" ShowStartingNode="false" />
</nav>
</header>
<div>
<asp:ContentPlaceHolder ID="MainContent" runat="server"></asp:ContentPlaceHolder>
</div>
<footer>
<p>© 2023 My Web Forms Site</p>
</footer>
</form>
</body>
</html>
2. 内容页面(Default.aspx):
<!-- Default.aspx -->
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication._Default" %>
<%@ PreviousPageType VirtualPath="~/Site.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<!-- 在这里添加特定于内容页面的头部内容 -->
<style>
body {
background-color: #f0f0f0;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Welcome to My Web Forms Site!</h2>
<p>This is the home page content.</p>
</asp:Content>
3. 站点地图文件(Web.sitemap):
<!-- Web.sitemap -->
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
<siteMapNode title="Home" url="~/Default.aspx">
<siteMapNode title="Products" url="~/Products.aspx">
<siteMapNode title="Laptops" url="~/Laptops.aspx" />
<siteMapNode title="Desktops" url="~/Desktops.aspx" />
</siteMapNode>
</siteMapNode>
</siteMap>
4. 站点配置文件(Web.config):
在 Web.config 文件中,确保启用了站点地图(site map):
<configuration>
<system.web>
<siteMap defaultProvider="XmlSiteMapProvider" enabled="true">
<providers>
<add name="XmlSiteMapProvider" type="System.Web.XmlSiteMapProvider" siteMapFile="Web.sitemap" />
</providers>
</siteMap>
<!-- 其他配置项 -->
</system.web>
</configuration>
这是一个简单的示例,演示了一个包含母版页、导航和内容页面的 Web Forms 应用程序。你可以根据实际需求对其进行扩展和定制。
转载请注明出处:http://www.pingtaimeng.com/article/detail/14999/ASP.NET Web Forms