从子母版页代码后面访问子母版页上的用户控件

发布于 2024-08-03 13:51:01 字数 1181 浏览 3 评论 0原文

尝试通过同一母版页的隐藏代码在我的子母版页上设置文字用户控件的值。

这是我正在使用的代码示例:

Global.master

<form id="form1" runat="server">
<div>
    <asp:ContentPlaceHolder id="GlobalContentPlaceHolderBody" runat="server">

    </asp:ContentPlaceHolder>
</div>
</form>

Template.master(Global.master 的子级)

<asp:Content ID="TemplateContentBody" ContentPlaceHolderID="GlobalContentPlaceHolderBody" Runat="Server">
<asp:Literal ID="MyLiteral1" runat="Server"></asp:Literal>

<p>This is template sample content!</p>
  <asp:ContentPlaceHolder ID="TemplateContentPlaceHolderBody" runat="server">

</asp:ContentPlaceHolder>

Template.master.cs

    protected void Page_Load(object sender, EventArgs e)
{
    MyLiteral1.Text = "Test";
}

ContentPage.aspx

< asp:Content ID="ContentBody" ContentPlaceHolderID="TemplateContentPlaceHolderBody" Runat="Server">
</asp:Content>

一旦我能够实现这一点,我还需要能够通过内容页访问全局和模板母版页上的内容。

Trying to set the value of a literal user control on my child master page via the code behind of the same master page.

Here is example of the code I am using:

Global.master

<form id="form1" runat="server">
<div>
    <asp:ContentPlaceHolder id="GlobalContentPlaceHolderBody" runat="server">

    </asp:ContentPlaceHolder>
</div>
</form>

Template.master (child of Global.master)

<asp:Content ID="TemplateContentBody" ContentPlaceHolderID="GlobalContentPlaceHolderBody" Runat="Server">
<asp:Literal ID="MyLiteral1" runat="Server"></asp:Literal>

<p>This is template sample content!</p>
  <asp:ContentPlaceHolder ID="TemplateContentPlaceHolderBody" runat="server">

</asp:ContentPlaceHolder>

Template.master.cs

    protected void Page_Load(object sender, EventArgs e)
{
    MyLiteral1.Text = "Test";
}

ContentPage.aspx

< asp:Content ID="ContentBody" ContentPlaceHolderID="TemplateContentPlaceHolderBody" Runat="Server">
</asp:Content>

Once I am able to achieve this, I will also need to be able to access content on the global and template master pages via content pages.

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

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

发布评论

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

评论(3

扮仙女 2024-08-10 13:51:01

如果我理解您的情况,您希望内容页访问母版页中的项目。如果是这样,您需要设置一个属性以从母版页公开它们,并且在内容页中您可以设置 MasterType 指令。

看看

If I understand your scenario you want to have your content pages access items from your master pages. If so, you'll need to setup a property to expose them from your master page, and in your content page you can setup a MasterType directive.

Take a look at this post for an example.

十二 2024-08-10 13:51:01

找到了一个可行的解决方案。在 template.master (嵌套子母版)中,我必须将代码放在 OnLoad 事件中。

protected override void OnLoad(EventArgs e)
{
    MyLiteral1.Text= "<p>MyLiteral1 Successfully updated from nested template!</p>";
    base.OnLoad(e);
}

非常奇怪...

基本上,我使用全局母版作为在每个页面上共享代码的页面,然后我将有各种嵌套页面来适合每个网站部分。对于导航嵌套模板,我希望能够显示用户是否已登录以及购物车中有多少商品。

如果有更好的方法来实现这一目标,我愿意接受建议。

Found a working solution. In the template.master (nested child master), I had to put the code in OnLoad event.

protected override void OnLoad(EventArgs e)
{
    MyLiteral1.Text= "<p>MyLiteral1 Successfully updated from nested template!</p>";
    base.OnLoad(e);
}

Very strange...

Basically, I am using the global master as the page that has code shared on every page, then I will have various nested pages to suit each website section. For the navigation nested template, I want to be able to show if the user is logged in and how many items in shopping cart.

If there is a better way to achieve this, I am open to suggestions.

爱给你人给你 2024-08-10 13:51:01

编辑:当我认为您试图从后面的父主代码访问子主的控件时,这是我的答案。

您可以使用递归 findControl 函数:

protected Control FindControlRecursive(string id, Control parent)
{
    // If parent is the control we're looking for, return it
    if (string.Compare(parent.ID, id, true) == 0)
        return parent;

    // Search through children
    foreach (Control child in parent.Controls)
    {
        Control match = FindControlRecursive(id, child);

        if (match != null)
            return match;
    }

    // If we reach here then no control with id was found
    return null;
}

然后在母版页中使用以下代码:

protected void Page_Load(object sender, EventArgs e)
{
//EDIT: if GlobalContentPlaceHolderBody isn't visible here, use this instead:
//Control c = FindControlRecursive("MyLiteral1", Page.FindControl("GlobalContentPlaceHolderBody"));
    Control c = FindControlRecursive("MyLiteral1", GlobalContentPlaceHolderBody);
    if(c != null)
        ((Literal)c).Text = "Test";
}

EDIT: This is my answer when I thought you were trying to access a control on the child master from the parent master code behind.

You can use a recursive findControl function:

protected Control FindControlRecursive(string id, Control parent)
{
    // If parent is the control we're looking for, return it
    if (string.Compare(parent.ID, id, true) == 0)
        return parent;

    // Search through children
    foreach (Control child in parent.Controls)
    {
        Control match = FindControlRecursive(id, child);

        if (match != null)
            return match;
    }

    // If we reach here then no control with id was found
    return null;
}

Then use this code in your master page:

protected void Page_Load(object sender, EventArgs e)
{
//EDIT: if GlobalContentPlaceHolderBody isn't visible here, use this instead:
//Control c = FindControlRecursive("MyLiteral1", Page.FindControl("GlobalContentPlaceHolderBody"));
    Control c = FindControlRecursive("MyLiteral1", GlobalContentPlaceHolderBody);
    if(c != null)
        ((Literal)c).Text = "Test";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文