在base.master上:
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Base.master.cs" Inherits="WebApplicationControlTest.Base" %>
<!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 runat="server">
<title>The title</title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
There is a content here: <br />
<asp:ContentPlaceHolder ID="body" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
关于嵌套母版
<%@ Master Language="C#" MasterPageFile="~/MasterPages/Base.Master" AutoEventWireup="true" CodeBehind="NestedMasterPageTest2.master.cs" Inherits="WebApplicationControlTest.MasterPages.NestedMasterPageTest2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="PlaceHolder" ContentPlaceHolderID="body" runat="server">
This is inside the NestedPage<br />
<asp:ContentPlaceHolder ID="PlaceHolderLeft" runat="server">
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="PlaceHolderRight" runat="server">
</asp:ContentPlaceHolder>
</asp:Content>
并在default.aspx上
<asp:Content ID="PlaceHolder" ContentPlaceHolderID="PlaceHolderLeft" runat="server">
This is a test!
</asp:Content>
在default.aspx.cs上
protected override void OnPreInit(EventArgs e){
Control control = LoadControl("TheUrlOfTheControl.ascx");
if (Page.Master.FindControl("body") != null) {
Page.Master.FindControl("body").Controls.Add(control);
}
}
我需要将控件添加到正文内容占位符的最后一部分,但 FindControl 确实返回 null...我们如何以编程方式将控件添加到母版页的内容占位符?
Thanks
已回复供以后参考
protected override void OnPreInit(EventArgs e){
Control control = LoadControl("TheUrlOfTheControl.ascx");
Control placeHolderControl = Page.FindControl("body");
if (placeHolderControl != null) {
placeHolderControl.Controls.Add(control);
} else {
MasterPage theMaster = Page.Master;
while (theMaster != null) {
placeHolderControl = theMaster.FindControl("body");
if (placeHolderControl != null) {
placeHolderControl.Controls.Add(control);
break;
}
theMaster = theMaster.Master;
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)