Brent Oliver Brent Oliver - 1 month ago 22
ASP.NET (C#) Question

Hide/show gridview column based on role

Want to be able to set an "Edit" linkbutton to visible=false unless the user has a role of "Editor".

Been poking around stackoverflow and elsewhere and so far have not been able to get this to work.

Gridview:

<asp:GridView ID="GridView1" runat="server" Caption="Questions Awaiting Review" AllowSorting="True" PagerSettings-Mode="NumericFirstLast"
OnPageIndexChanging="GridView1_PageIndexChanging" CaptionAlign="Top" EmptyDataText="No Questions Pending Review."
PageSize="10" AllowPaging="true" PagerStyle-HorizontalAlign="Center" PagerStyle-Font-Size="Large" DataKeyNames="QuestionID"
OnRowCommand="GridView1_RowCommand" AutoGenerateColumns="false" AlternatingRowStyle-BackColor="#cccccc"
OnPreRender="GridView1_OnPreRender">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="QuestionID" runat="server" Text='<%# Eval("QuestionID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="KeyObjective" HeaderText="Key Objective" ItemStyle-Width="250" />
<asp:BoundField DataField="SubmitDate" HeaderText="Submitted Date" ItemStyle-Width="60" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="Details" CommandArgument='<%# Eval("QuestionID") %>' runat="server" CommandName="viewQuestion">View Question</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="Edit" CommandArgument='<%# Eval("QuestionID") %>' runat="server" CommandName="editQuestion">Edit Question</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>

</asp:GridView>


Have changed the code behind to use OnPreRender for the gridview, which if the value is hardcoded hides the column. However when I try to retrieve the is in role of editor then the value does not seem to be evaluating correctly. Always returns false even when the user has a role of Editor.

protected void GridView1_OnPreRender(object sender, EventArgs e)
{
if (Roles.IsUserInRole("Editor"))
{
// Enter correct column index.
GridView1.Columns[4].Visible = true;
}
else
{
GridView1.Columns[4].Visible = false;
}
}


Hoping I'm missing something simple, new to asp.net so not unlikely.

Win Win
Answer

You want to show/hide an entire column instead of LinkButton control. Otherwise, unauthorized user will always see a column with blank cells which is odd.

The following example will hide an entire column.

Screen Shot (Authorize vs Unauthorized)

enter image description here

ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DemoWebForm.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1"
            runat="server"
            DataKeyNames="QuestionID"
            OnPreRender="GridView1_OnPreRender"
            AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="QuestionID" runat="server" Text='<%# Eval("QuestionID") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="KeyObjective" HeaderText="Key Objective" ItemStyle-Width="250" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="Edit" CommandArgument='<%# Eval("QuestionID") %>'
                            runat="server" CommandName="editQuestion">Edit Question</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </form>
</body>
</html>

Code Behind

public class Question
{
    public int QuestionID { get; set; }
    public string KeyObjective { get; set; }
}

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridView1.DataSource = new List<Question>
            {
                new Question {QuestionID = 1, KeyObjective = "One"},
                new Question {QuestionID = 2, KeyObjective = "Two"},
                new Question {QuestionID = 3, KeyObjective = "Three"},
            };
            GridView1.DataBind();
        }
    }

    protected void GridView1_OnPreRender(object sender, EventArgs e)
    {
        bool isEditor = true; // Business logic here
        if (isEditor)
        {
            // Enter correct column index.
            GridView1.Columns[2].Visible = false;
        }
    }

}