R – ASP.NET Membership & GridViews

asp.netasp.net-membershipgridview

I have a site using ASP.NET Membership and have a number of roles that users are assigned to. One feature I really want to do is to be able to programatically add the "edit" or "delete" buttons to rows in a GridView, based on the role the user is a member of. For example:

If the user is an admin I want to show the edit and delete buttons on the gridview, but if they are an editor, to just show the edit button. I know this is possible as I have seen an example done before, but can't for the life of me find it in my bookmarks!

Thanks in advance.

Best Answer

Handle the "RowDataBound" event for your gridview, and inside your event handler, check the user's credentials and set the appropriate visibility on your buttons.

void MyGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{  
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // Check user credentials and set button visibility
      e.Row.Cells[x].Controls[y].Visible = true; //or false
    }
}
Related Topic