如何以编程方式将控件添加到母版页的内容占位符

发布于 2024-08-14 17:19:23 字数 2095 浏览 2 评论 0原文

在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上

<%@ 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.cs

<asp:Content ID="PlaceHolder" ContentPlaceHolderID="PlaceHolderLeft" runat="server">
    This is a test!
</asp:Content>

上的default.aspx

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...我们如何添加以编程方式控制母版页的内容占位符?

谢谢

On the 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>

On the nested master

<%@ 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>

and on the default.aspx

<asp:Content ID="PlaceHolder" ContentPlaceHolderID="PlaceHolderLeft" runat="server">
    This is a test!
</asp:Content>

on the 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);
    }
}

I need to add the control to the last part of the body content Placeholder but FindControl does return null... how can we add controls to the masterpage's content placeholder programmatically?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦中的蝴蝶 2024-08-21 17:19:23

已回复供以后参考

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;
        }
    }
}

Answered for future reference

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;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文