Enable or disable the delete link in the GridView according to the role of the logged in user

In some scenarios you may only want users of a particular role to delete records from the GridView. So you need a mechanism to disable/hide the delete link in the gridview based on roles. The code below will enable the delete link only for “Admin” roles and disable it for the rest.


Protected Sub gridView1_RowDataBound(ByVal sender As Object, ByVal e As system.Web.UI.WebControls.GridViewRowEventArgs) Handles Gridview1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
If User.IsInRole("Admin") Then
Dim DeleteLink As LinkButton = CType(e.Row.FindControl("HyperLinkDelete"), LinkButton)
DeleteLink.Enabled = true
else
DeleteLink.Enabled = True
End If
End If
End Sub

One Response to “Enable or disable the delete link in the GridView according to the role of the logged in user”

  1. Thank u so much….it hepled..:)

Leave a Reply