从母版页的代码隐藏(vb.net)

时间:2015-10-06 17:22:33

标签: asp.net vb.net master-pages

我正在导航列表项中创建徽章通知。我希望能够从母版页后面的代码更新标签。我被困在这里是我的代码。

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

以下是我的代码隐藏文件中的代码。

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

请帮助

2 个答案:

答案 0 :(得分:0)

您的LoginView是一个模板化控件,因此,您尝试时无法访问控件。 FindControl通常有效,但在这种情况下,只搜索直接子控件。

所以你可能会做这样的事情(用你的LoginView的ID替换MyLoginViewID

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

以下是我之前使用过的方法,我的回答受到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

答案 1 :(得分:0)

我找到了解决方案

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