locked
Accessing a label within a list on a master page. RRS feed

  • Question

  • User202799896 posted

    I have started a project using the default asp setup and copied to Site.Master page and renamed it to Account.Site for use in a different part. I have added an icon and label in one of the li elements. From a page that uses this master how do i update the label. i currently have this code. I am trying to edit the MailCount label.

    Master page

    <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Account.Master.cs" Inherits="BiteBoardWeb.SiteMaster" %>

    <!DOCTYPE html>

    .....

    <AnonymousTemplate>
        <ul class="nav navbar-nav navbar-right">
            <li><asp:HyperLink runat="server" ID="Messages" NavigationUrl="~/Login">
                <asp:Image ID="Image1" runat="server" ImageUrl='http://www.biteboard.ca\images\Mail-closed.png' Width="30%" Height="30%"/>

                <asp:Label ID="MailCount" runat="server" Text="(0)"></asp:Label></asp:HyperLink>

            </li>
    <li><a runat="server" href="/Default">Log out</a></li>
    </ul>
    </AnonymousTemplate>

    my code to update it

    Label label = (Label)Page.Master.FindControl("MailCount");
    if (label != null)
    label.Text = "(" + count + ")";

    it always comes back null. what am i missing?

    thanks

    Sunday, April 16, 2017 11:08 PM

Answers

  • User-2057865890 posted

    Hi larryse,

    Master page

    <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Account.Master.cs" Inherits="BiteBoardWeb.SiteMaster" %>

    <!DOCTYPE html>

    .....

    <AnonymousTemplate>
        <ul class="nav navbar-nav navbar-right">
            <li><asp:HyperLink runat="server" ID="Messages" NavigationUrl="~/Login">
                <asp:Image ID="Image1" runat="server" ImageUrl='http://www.biteboard.ca\images\Mail-closed.png' Width="30%" Height="30%"/>

                <asp:Label ID="MailCount" runat="server" Text="(0)"></asp:Label></asp:HyperLink>

            </li>
    <li><a runat="server" href="/Default">Log out</a></li>
    </ul>
    </AnonymousTemplate>

    my code to update it

    Label label = (Label)Page.Master.FindControl("MailCount");
    if (label != null)
    label.Text = "(" + count + ")";

    it always comes back null. what am i missing?

    If content page consists of Login control with in LoginView and content page inherits MasterPage. You could refer the following code snippets to find control.

    protected void Page_Load(object sender, EventArgs e)
    {
        //Find LoginName control with in AnonymousTemplate of LoginView control
        LoginView lv = (LoginView)Page.Master.FindControl("LoginView1");
        if (lv != null)
        {
            var count = 1;
            Label label = (Label)lv.FindControl("MailCount");
            if (label != null)
                label.Text = "(" + count + ")";
        }
    }

    master page

    <div>
        <asp:LoginView ID="LoginView1" runat="server">
            <AnonymousTemplate>
                <ul class="nav navbar-nav navbar-right">
                    <li>
                        <asp:HyperLink runat="server" ID="Messages" NavigationUrl="~/Login">
                           <asp:Image ID="Image1" runat="server" ImageUrl='http://www.biteboard.ca\images\Mail-closed.png' Width="30%" Height="30%" />
                            <asp:Label ID="MailCount" runat="server" Text="(0)"></asp:Label>
                        </asp:HyperLink>
                    </li>
                    <li><a runat="server" href="/Default">Log out</a></li>
                </ul>
            </AnonymousTemplate>
        </asp:LoginView>
    </div>

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 17, 2017 9:51 AM