简体   繁体   English

从母版页的背后代码(vb.net)更改母版页上的标签文本

[英]Change label text on master page from the Master page's Code Behind (vb.net)

I am creating a badge notification in a navigation list item. 我正在导航列表项中创建徽章通知。 I would like to be able to update the label from the code behind of the master page. 我希望能够从母版页后面的代码中更新标签。 I am stuck here is my code. 我被困在这里是我的代码。

<asp:RoleGroup Roles="Admin">
                                <ContentTemplate>
                                    <ul class="nav navbar-nav navbar-right">
                                        <%--<li><a runat="server" href="~/">Home</a></li>--%>
                                        <li class="dropdown">
                                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Submissions<span class="caret"></span></a>
                                            <ul class="dropdown-menu" role="menu">
                                                <li><a runat="server" href="Submissions">Submissions</a></li>
                                                <li><a runat="server" href="SubmissionEmails">Emails</a></li>
                                                <li><a runat="server" href="SubmissionEmailTemplate">Email Template</a></li>
                                            </ul>
                                        </li>
                                        <li><a runat="server" href="Faults">Faults</a></li>
                                        <li class="dropdown">
                                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Reporting<span class="caret"></span></a>
                                            <ul class="dropdown-menu" role="menu">
                                                <li><a runat="server" href="CompanyDeclaration">Declaration</a></li>
                                                <li><a runat="server" href="CompanyDeclarationSummary">Summary</a></li>
                                                <li><a runat="server" href="CompanyTemplate">Template</a></li>
                                            </ul>
                                        </li>
                                        <li class="dropdown">
                                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><img src="/images/person.png"> <%: Context.User.Identity.Name()%> <label class="badge badge-danger" ID="Count" runat="server">5</label><span class="caret"></span></a>
                                            <ul class="dropdown-menu" role="menu">
                                                <li><a runat="server" href="~/Admin/FaultCodeRules">Fault Code Rules</a></li>
                                                <li><a <a runat="server" href="~/Admin/Smelters">Standard Smelters</a></li>
                                                <%--<li><a href="#">Back Flush</a></li>--%>
                                                <%--<li><a runat="server" href="SalesData">Sales Data</a></li>--%>
                                                <%--<li><a runat="server" href="Vendor">Vendor</a></li>--%>
                                                <%--<li><a runat="server" href="Usage">WebSite Usage</a></li>--%>
                                                <li class="divider"></li>
                                                <li class="dropdown-header">Admin Tools</li>
                                                <li><a runat="server" href="~/Admin/PendingRegistrations">Pending Registrations <label class="badge badge-danger" ID="UCount" runat="server">13</label></a></li>
                                                <li><a runat="server" href="~/Admin/AssignRoles">Assign Roles</a></li>
                                                <li><a runat="server" href="~/Admin/AddFactory">Add New Factory</a></li>
                                                <li><a runat="server" href="~/Admin/Register">Register User</a></li>
                                                <li><a runat="server" href="~/Admin/SwitchUser">View as another Factory</a></li>
                                                <li class="divider"></li>
                                                <li><a runat="server" href="~/Account/Manage" title="Manage your account">Manage Your Account</a></li>                                              
                                                <li>
                                                   <asp:LoginStatus runat="server" LogoutAction="Redirect" LogoutText="Log off" LogoutPageUrl="~/" OnLoggingOut="Unnamed_LoggingOut" />
                                               </li>
                                            </ul>
                                        </li>
                                        <li>
                                            <a runat="server" href="Default"><img alt="Home" src="/images/Home-24.png"></a>
                                        </li>
                                    </ul>
                                </ContentTemplate>
                            </asp:RoleGroup>
  Snippet 
<li><a runat="server" href="~/Admin/PendingRegistrations">Pending Registrations <asp:Label class="badge badge-danger" ID="Label1" runat="server" /></a></li>

Here is the code in my code behind file. 这是我的代码隐藏文件中的代码。

Dim connectionString As String = ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString
Dim con As New SqlConnection(connectionString)
Dim cmd As SqlCommand = New SqlCommand
cmd.Connection = con
con.Open()
cmd.CommandText = "Select Count(*) From User_Register"
Dim UserCount = cmd.ExecuteScalar()
con.Close()
label1.text = UserCount

Please Help 请帮忙

Your LoginView is a templated control and as such, the controls cannot be accessed as you're trying. 您的LoginView是模板控件,因此无法在尝试时访问这些控件。 FindControl normally works, but in this case, only searches direct child controls. FindControl通常可以正常工作,但在这种情况下,仅搜索直接子控件。

So you might do something like this (replacing MyLoginViewID with your LoginView's ID: 因此,您可能会执行以下操作(将MyLoginViewID替换为LoginView的ID:

Dim label as Label = CType(FindControlRecursive(MyLoginViewID, "Label1"), Label)
label.Text = UserCount.ToString()

Below is a method I've used before, and my answer was inspired by this answer 以下是我以前使用过的方法,我的答案受到了这个答案的启发

''' <summary>
    ''' Recursively loops through all containers in a control looking for the specified control.
    ''' </summary>
    ''' <param name="callingControl"></param>
    ''' <param name="controlId"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function FindControlRecursive(ByVal callingControl As System.Web.UI.Control, ByVal controlId As String) As System.Web.UI.Control

        If callingControl Is Nothing Then Return Nothing

        Dim ctrl As Control = callingControl.FindControl(controlId)

        If ctrl Is Nothing Then
            For Each child As Control In callingControl.Controls
                ctrl = FindControlRecursive(child, controlId)
                If ctrl IsNot Nothing Then Exit For
            Next
        End If

        Return ctrl

    End Function

I found a solution 我找到了解决方案

Dim lv As LoginView = DirectCast(FindControl("myLoginView"), LoginView)
                Dim ln As Label = DirectCast(lv.FindControl("Count"), Label)
                ln.Text = UserCount.ToString

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM