I have this grid view which contains details of users. How can I disable the Delete Button for the logged in user (i.e. the current user not allowed to delete him/her self, but he/she can delete other users) .
I have added the following code to the RowDataBount event but it is doing nothing.
Code:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="UsersObjectDataSource" >
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="Username" HeaderText="Username" SortExpression="Username" />
<asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName" />
<asp:TemplateField HeaderText="Options">
<ItemTemplate>
<table>
<tr>
<td>
<asp:HyperLink ID="View" runat="server" NavigateUrl='<%# "~/Users/User.aspx?A=1&I=" + Eval("ID")%>' Text="View" />
</td>
<td>
<asp:HyperLink ID="Edit" runat="server" NavigateUrl='<%# "~/ Users /User.aspx?A=2&I=" + Eval("ID") %>' Text="Edit" />
</td>
<td>
<asp:LinkButton ID="Delete" runat="server" CommandName="Delete" Text="Delete" OnClientClick='<%# "return confirm(\"Are you sure you want to delete this User (" + Eval("UserName")")?");" %>' />
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
string userId = HttpContext.Current.User.Identity.ToString();
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[0].Text== userId )
{
e.Row.Cells[4].FindControl("Delete").Visible =false; }
}