【问题标题】:c# Accessing MasterPage elements from Child Pagec# 从子页面访问 MasterPage 元素
【发布时间】:2013-08-22 10:26:37
【问题描述】:

我在网上查看了几个示例,但似乎没有一个对我有用。

我要做的就是从母版页标题访问标签的内容。

这就是我所拥有的......

内容页面上的标签

<asp:Label ID="StaffUserName" runat="Server" />

MasterPage 上名为“ThisLoginName”的标签

    <header>
        <div class="content-wrapper">
            <div class="float-left">
                <p class="site-title">
                    <a runat="server" href="~/">Home</a>
                </p>
            </div>
            <div class="float-right">
            <section id="login">
                Welcome! <b><asp:Label ID="ThisLoginName" runat="server" /></b>

            </section>
            <nav>
                <ul id="menu">
                    <li><a runat="server" href="~/">Home</a></li>
                    <li><a runat="server" href="~/Admin">Admin</a></li>

                </ul>
            </nav>
        </div>
    </div>
</header>

我已经阅读了一些在线教程,但我似乎无法解决这个问题。但是,我的内容页面顶部确实有这个

<%@ MasterType VirtualPath="~/Site.Master" %>

如果有人能帮忙,我将不胜感激。

【问题讨论】:

    标签: c# asp.net .net


    【解决方案1】:
    【解决方案2】:

    您需要在母版页类中添加公共属性:

    public string LabelText 
    { 
    get { return StaffUserName.Text; } // StaffUserName is the ID of your LABEL
    set { StaffUserName.Text = value; } 
    } 
    

    看到你已经添加了:&lt;%@ MasterType VirtualPath="~/Site.Master" %&gt;,所以在你的内容页面访问这个:

    Master.LabelText = "MyText";
    

    string test= Master.LabelText;
    

    【讨论】:

      【解决方案3】:

      固定..

      这是因为我从 Windows 登录中提取用户名的代码是在母版页的页面加载时运行的。

      所以我把它移到了

      public void master_Page_PreLoad(object sender, EventArgs e)
      

      部分,然后它起作用了。

      所以它一定一直在工作,只是在一个空旷的地方拉动。

      谢谢

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-12-27
        • 2011-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-15
        • 1970-01-01
        相关资源
        最近更新 更多