none
How can I control row visibility in GridView by matching NT Group name from a database table? RRS feed

  • Question

  • I am trying to control the label controls from the ItemTemplateField in the GridView, each row contains a label control and a group name.

    For example the gridview looks like this:

    Group A label 1
    Group B label 2
    Group C label 3

    I am using VB Function to control the label based on NT user group name. If the NT user from Group A then only the first row is visible and the rest are hidden, If the user from Group B then only 2nd row is visible.

    html page:

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="default.aspx.vb" Inherits="Control_Systems._default" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Applications</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ControlDS" BorderStyle="None" ShowHeader="False">
                <Columns>
                    <asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="NT_GROUP" SortExpression="NT_GROUP">
                        <ItemTemplate>
                            <asp:Label ID="lblGroup" runat="server" Text='<%# Eval("NT_GROUP") %>' Visible="False"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="App_Name" SortExpression="App_Name">
                        <ItemTemplate>
                            <asp:Label ID="lblAppName" runat="server" Text='<%# Bind("App_Name") %>' Visible="False"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Url" SortExpression="Url">
                        <ItemTemplate>
                            <asp:HyperLink ID="hLnk" runat="server" NavigateUrl='<%# Eval("Url") %>' Text='<%# Eval("Url") %>' Target="_blank" Visible="False"></asp:HyperLink>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <br />
            <asp:SqlDataSource ID="ControlDS" runat="server" ConnectionString="<%$ ConnectionStrings:ControlCS %>" SelectCommand="SELECT NT_GROUP, App_Name, RTRIM(App_Web_Link) AS Url FROM VW_WebLinks"></asp:SqlDataSource>
        
        </div>
        </form>
    </body>
    </html>

    code page:

    Public Class _default
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        End Sub
    
        Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim hLink As HyperLink = TryCast(e.Row.FindControl("hLnk"), HyperLink)
                Dim lblApp As Label = TryCast(e.Row.FindControl("lblAppName"), Label)
                Dim lblGrp As Label = TryCast(e.Row.FindControl("lblGroup"), Label)
    
                If IsInGroup("Group A") Then  '----------> Must match record from the table
    
                    hLink.Visible = True
                    lblApp.Visible = True
                    lblGrp.Visible = True
    
                Else
                    e.Row.Visible = False '----------> Make rows hidden if user is not in group
    
                End If
    
            End If
    
        End Sub
    
    
    
        Public Function IsInGroup(ByVal GroupName As String) As Boolean
    
            Dim MyIdentity As System.Security.Principal.WindowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent()
            Dim MyPrincipal As System.Security.Principal.WindowsPrincipal = New System.Security.Principal.WindowsPrincipal(MyIdentity)
            Return MyPrincipal.IsInRole(GroupName)
    
        End Function
    
    End Class

    TSQL View:
    NT_Group App_Name Url
    Group A link 1
    Group B link 2
    Group C link 3

    Is it possible to get the GridView1_RowDataBound to check if the authenticated NT user group matches the NT_Group row, then display the row that only corresponds to the user group and hide other rows?

    I appreciate any help.

    • Moved by Stanly Fan Tuesday, April 17, 2018 7:16 AM
    Monday, April 16, 2018 4:57 AM

All replies

  • Hi Matt,

    According to your description, your issue is more related about ASP.NET WebForm. And this forum is discussing and asking questions about the Windows Form and ClickOnce, it will be more appropriate to ask your question at ASP.NET Forums.

    Thank you for your understanding.

    Regards,

    Frankie


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 16, 2018 7:56 AM