MasterPage嵌套及MasterPage中的控件和变量的访问
1. 嵌套母版页
(1) 主母版页 MainMasterPage.
master
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MainMasterPage.master.cs" Inherits="MainMasterPage" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> </head> <body leftmargin="0" topmargin="0"> <form id="form1" runat="server"> <div align="center"> <table width="763" height="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF"> <tr> <td width="763" valign="top"> <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder> </td> </tr> </table> </div> </form> </body> </html> |
(2)子母版页 (这种只能手动创建这文件)
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="SubMasterPage.master.cs" MasterPageFile="~/MainMasterPage.master" Inherits="SubMasterPage" %> <asp:content id="Content1" contentplaceholderid="ContentPlaceHolder1" runat="server"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="244" valign="bottom"> <img src="images/pagepic.gif" width="244" height="223"> <h1> 以上内容来自子母版页</h1> </td> <td valign="top" align="left"> <asp:ContentPlaceHolder ID="ContentPlaceHolder2" Runat="server"> </asp:ContentPlaceHolder></td> </tr> </table> </asp:content> |
(3)内容页
<%@ Page Language="C#" MasterPageFile="~/SubMasterPage.master" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="Index" Title="示例5-2" %>
<asp:Content ID="Content2" ContentPlaceHolderID="contentPlaceHolder2" runat="server"> <p> </p> <p> </p> <h1> 网站介绍</h1> <p> 本页面采用来自ASP.NET 2.0技术的母版页新特性进行开发。 主要包括两个页面:母版页和内容页。 母版页后缀名是.master,其封装网站中的共用元素。 内容页实际是普通的.aspx文件,它包含除母版页的其他内容。 在运行时,ASP.NET引擎将两种页面内容合并执行,最后将结果发给客户端浏览器。</p> <br> <h1> 以上内容来自内容页</h1> </asp:Content>
|
2. 访问母版页控件和属性
(1) 母版页后台代码访问
在母版页前台页面中添加一个服务器控件
<asp:Label ID="LabelInMaster" runat="server"></asp:Label>
然后在母版页的Page_Load事件中写代码
protected void Page_Load(object sender, EventArgs e)
{
LabelInMaster.Text = "现在时间:" + System.DateTime.Now.ToShortTimeString();
}
(2) 在内容页面中调用母版页控件
在母版添加控件和属性
<asp:Label ID="Label1" runat="server"></asp:Label>
public Label MasterPageLabel
{
get
{
return Label1;
}
set
{
Label1 = value;
}
}
先要在内容页面中添加
<%@ MasterType VirtualPath="~/MasterPage22.master" %>
然后后台才能调用
protected void Page_Load(object sender, EventArgs e)
{
Master.MasterPageLabel.Text = "现在时间:" + System.DateTime.Now.ToShortTimeString();
Master.MasterPageLabel.Font.Size = 20;
}
(3) 同上,内容页面设置变量的值(绑定值)
在母版添加绑定标识、变量和属性
<%= LabelText %>
string _labelText = "";
public String LabelText
{
get
{
return _labelText;
}
set
{
_labelText = value;
}
}
同上在内容页面里加上:
<%@ MasterType VirtualPath="~/MasterPage33.master" %>
在后台代码中
protected void Page_Load(object sender, EventArgs e)
{
Master.LabelText = "现在时间:" + System.DateTime.Now.ToShortTimeString();
}
分享到:
相关推荐
masterpage +theme 轻松实现切换模板实例
MasterPage 与 ASP.NET Content Page 相互调用
一个visual studio 2015中关于母版页(masterpage)的例子,包括了母版页的创建使用,和套用母版页的其他页面的例子
asp.net masterpage 模板页
天轰穿系列教程之-60MasterPage母版页创建 天轰穿系列教程之-60MasterPage母版页创建 天轰穿系列教程之-60MasterPage母版页创建 天轰穿系列教程之-60MasterPage母版页创建 天轰穿系列教程之-60MasterPage母版页创建 ...
天轰穿系列教程之-61MasterPage母版页技巧天轰穿系列教程之-61MasterPage母版页技巧天轰穿系列教程之-61MasterPage母版页技巧天轰穿系列教程之-61MasterPage母版页技巧天轰穿系列教程之-61MasterPage母版页技巧天轰...
ASP.NET源码——母板页(MasterPage)中设置主题(附程序详解).zip
移动端日期和时间选择控件lCalendar-master,可供学习设计参考。
2.添加一个masterpage页 ,mastepage页包括 head.html部分和left.html部分 3.做个引用了刚写的masterpage页的的网页Content.aspx.页面风格跟模板中的list.html页一致,页面样式通过页面的stylesheettheme中引入皮肤...
可是网上有很多人说这个东西鸡肋,说这个东西在 .aspx页面中使用还行,却不能在母板页(MasterPage)中使用。是的,确实不能直接在MasterPage通过一个DDL的选择来切换整个网站的主题,因为 Page.Theme 这个属性都是...
实验做得成果,有需要的话可以看看 功能一般 不复杂
master page search problem viewdata js
MasterPage, Page, UserControl 如何互动,传值的例子。网上找到的
masterpage使用视频。官方的比较好 masterpage使用视频。官方的比较好
C#Winform控件库NetWinformControl-master.zip,内有源代码,demo,相关网站。
SharePoint Server 2013 Master page
Master Page used to customize global navigation
基于C# 平台的自定义控件 HZHControl
how to update the CSS of the master page: This guid tells you how to update the CSS file so that to change the style of the menu which shows in the master page.
HoloEverywhere-master自定义控件大全.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。