views:

8

answers:

1

I have a data bound GridView control, in which I am able to disable individual cells based on the User role. This only works on the first page.

private void LimitAccessToGridFields()
    {
        if (User.IsInRole("Processing")) return;

        foreach (GridViewRow gridViewRow in gvScrubbed.Rows)
        {
            var checkBox = ((CheckBox) gridViewRow.FindControl("cbScrubbed"));
            checkBox.Enabled = false;

            // ButtonField does not have an ID to FindControl with
            // Must use hard-coded Cell index
            gridViewRow.Cells[1].Enabled = false; 
        }
    }

I call this method on Page_Load, where it works. I've tried it in the PageIndexChaging and PageIndexChanged event handlers, where it doesn't work. While debugging, it appears to successfully set Enabled to false in both of the controls in the row. My goal is to disable these fields, depending on user role, after changing the page. How should this be accomplished?

A: 

I found that this must be done in the RowDataBound event handler.

if (e.Row.RowType == DataControlRowType.DataRow)
{
   // details elided ...

   // Limits the access to grid fields.
   if (!User.IsInRole("PROCESSING"))
   {
       cbstuff.Enabled = false; // a checkbox
       e.Row.Cells[1].Enabled = false; //a link button
   }
}
Blanthor