Toggle GridView Delete Link based on User IsInRole

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

I have a GridView that I need to toggle the Delete Link "enabled" on
whether or not a user is in a specific Role. Any takers?

Your help is very much apprieciated!

Thanks
 
Dave said:
I have a GridView that I need to toggle the Delete Link "enabled" on
whether or not a user is in a specific Role. Any takers?

First use the "Edit Columns" dialog in the designer to change the
DeleteButton into a Templated Item if you haven't done this already. Then
edit its source code and add
Enabled='<%#User.IsInRole("whatever")%>'

If this doesn't work, you can always change the Enabled property of your
link in the code-behind, inside the GridView1_RowDataBound event.
 
Alberto, Thanks for the quick response. Can you elaborate on the Code
Behind? There are actually 3 roles that get to delete. If you can
provide an example it would help tremendously.


Thanks
 
Dave said:
Thanks for the quick response. Can you elaborate on the Code
Behind? There are actually 3 roles that get to delete. If you can
provide an example it would help tremendously.

I was thinking about something along these lines:

void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
((LinkButton)e.Row.Cells[1].Controls[1]).Enabled =
User.IsInRole(...) || User.IsInRole(...) || User.IsInRole(...);
}
}

Of course, instead of Cells[1].Controls[1] you will need to insert the
correct cell and control index to match the design of your grid. If you
don't know the numbers, or you plan to modify the design frequently, you can
use FindControl(name) instead.
 
I probably should have done this the first time. Below is my
GridView. The example you provided above produces the following
Error:

Specified argument was out of the range of valid values.
Parameter name: index

<asp:GridView ID="NotesGridView" runat="server" AllowPaging="True"
AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="fnID"
DataSourceID="GridViewSqlDataSource"
PageSize="6"

onselectedindexchanged="NotesGridView_SelectedIndexChanged"
onrowdatabound="NotesGridView_RowDataBound">
<Columns>
<asp:CommandField ShowDeleteButton="True"
ShowSelectButton="True" />
<asp:BoundField DataField="fnID"
HeaderText="fnID" InsertVisible="False"
ReadOnly="True" SortExpression="fnID"
Visible="False" />
<asp:BoundField DataField="fID"
HeaderText="fID" SortExpression="fID"
Visible="False" />
<asp:BoundField DataField="Type"
HeaderText="Type" SortExpression="Type" />
<asp:BoundField DataField="Note"
HeaderText="Note" SortExpression="Note" >
<ControlStyle Height="50px" Width="300px" /</asp:BoundField>
<asp:CheckBoxField DataField="Display"
HeaderText="Display"
SortExpression="Display" />
</Columns>
</asp:GridView>











Thanks for the quick response.  Can you elaborate on the Code
Behind?  There are actually 3 roles that get to delete.  If you can
provide an example it would help tremendously.

I was thinking about something along these lines:

  void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
  {
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      ((LinkButton)e.Row.Cells[1].Controls[1]).Enabled =
        User.IsInRole(...) || User.IsInRole(...) || User.IsInRole(...);
    }
  }

Of course, instead of Cells[1].Controls[1] you will need to insert the
correct cell and control index to match the design of your grid. If you
don't know the numbers, or you plan to modify the design frequently, you can
use FindControl(name) instead.
 
Dave said:
I probably should have done this the first time. Below is my
GridView. The example you provided above produces the following
Error:

Specified argument was out of the range of valid values.
Parameter name: index

Obviously. If you didn't change my example to provide correct indices, it
is bound to give such an error. Your delete command is in the first column
of your grid, so it would be Cells[0]. Then you need to determine the number
of the control in that column. Alas, since it is not a templated column, the
controls that will actually be generated are up to the asp.net runtime, so
we don't know a priori the index for the link. I presume that it is going to
be Controls[0], but you will need to ascertain that by means of the
debugger. Just place a breakpoint at the line where the control is accessed
and use the debugger to examine the contents of e.Row.Cells[0] to determine
the controls that it contains.
Note also that I have cast the control into a LinkButton, but from the
design of your grid I think that you are going to get a standard Button, so
you will need to adjust the cast accordingly. Once more, the debugger should
tell you this, since it will display not only the controls in the Cell but
also their types.
 
On Oct 25, 2:04 pm, "Alberto Poblacion" <earthling-
(e-mail address removed)> wrote:


Thanks for the lesson! That got it. I appreciate your patience!
 
On Oct 25, 2:04 pm, "Alberto Poblacion" <earthling-

Alberto, I hope there's room for one more question? I'm trying to get
my fingers totally wrapped around this. In the GridView there are 3
possible links. They appear to be all in the same Cell. So I'm
assuming that the Cell stays 0. When I try to change the Control
number in the second Array, or greater, I error. What am I doing
wrong?


bool a = User.IsInRole("Admin") || User.IsInRole("Site") ||
User.IsInRole("CompanyAdmin");
if (e.Row.RowType == DataControlRowType.DataRow)
{

//Edit Link
((LinkButton)e.Row.Cells[0].Controls[0]).Enabled = a;

//DeleteLink
((LinkButton)e.Row.Cells[0].Controls[0]).Enabled = a;

//Select Link
((LinkButton)e.Row.Cells[0].Controls[0]).Enabled = a;
}


Thanks again for the tutelage!
 
Dave said:
I hope there's room for one more question? I'm trying to get
my fingers totally wrapped around this. In the GridView there are 3
possible links. They appear to be all in the same Cell. So I'm
assuming that the Cell stays 0. When I try to change the Control
number in the second Array, or greater, I error. What am I doing
wrong?

You *should* be able to change the index in the Controls array. However,
the precise value that you enter is not immediately obvious and you need to
find it by means of the debugger.
For instance, if asp.net is rendering first a linkbutton, then a space,
and then a second linkbutton, then the first link is Controls[0], it is
followed by a LiteralControl (to hold the space) which is Controls[1], and
then your second link is Controls[2]. So if you were thinking that the
second link was Controls[1], you were wrong. In fact, it would throw an
error when attempting to cast the LiteralControl into LinkButton.

The exact controls that are being renedered are not obvious from the
source code; the easiest thing you can do is to use the debugger to look at
the collection. Or, if you prefer, you can change the CommandField into a
Templated field, assign known IDs to each link, and then address them by
means of FindControl:

((LinkButton)e.Row.FindControl("TheIdOfMyControl")).Enabled = a;
 
Back
Top