0

I have a Gridview with ItemTemplate using some Labels and 3 Button (Deactivate, Delete and Edit) as seen in picture below:

enter image description here

I want to hide these buttons from some users base on their username (Label UserName on Gridview), for example:

  • If UserName == "some string" then hide the Deactivate, Delete and Edit buttons

How do I do this in code behind RowDataBound event?

Ronaldinho Learn Coding
  • 13,254
  • 24
  • 83
  • 110

2 Answers2

1

Yes by using Gridview RowDataBound event you can do that

protected void grd1_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
        Label lbl=e.Row.FindControl("your cotrol Id")as Label;
       if(lbl!=null && lbl.Text.Trim()=="some string")
       {
           e.Row.FindControl("deactivate btn Id").Visible = false;
           e.Row.FindControl("delete btn Id").Visible = false;
           e.Row.FindControl("edit btn Id").Visible = false;
       }
   }
 }
Rajesh
  • 208
  • 1
  • 4
1

You can get the row data by column index, in the RowDataBound event you need to check if its a data row, not a header or footer..etc

if(e.Row.RowType == DataControlRowType.DataRow)
{
  if(e.Row.Cells[2].Text = "some string")
  {
    Button delete = (Button)e.Row.FindControl("control id to hide");
    delete.Visisble = false;
  }
}
Rami
  • 11
  • 1