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. FindControl
normally works, but in this case, only searches direct child controls.
So you might do something like this (replacing MyLoginViewID
with your LoginView's 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.