Enable and disable link button on gridview

前端 未结 2 1374
悲哀的现实
悲哀的现实 2021-01-19 19:12

I wants to enable or disable linkbutton on some rows of gridview based on condition.. Can i enable linkbutton on one row and disable it on another row of same grid view ??my

2条回答
  •  死守一世寂寞
    2021-01-19 19:46

    Yes you can easily do it in RowdataBound Event, but you have used lnk2.Visible property in your code.

    you may be using Visible property for another requirement but just want to confirm you that it is used to show/hide the Linkbutton only. To enable/disble a Linkbutton, use Enabled property of Linkbutton. as:

    lnk2.Enabled = true;// to enable linkbutton.
    lnk2.Enabled = false;// to disable linkbutton.
    

    If You want to do it using rowindex, then you can e.Row.RowIndex to find the current row index inside 'RowDatabound` event of gridview. as:

    if(e.Row.RowIndex==2)
    {
      LinkButton lnk2 = (LinkButton)e.Row.FindControl("LinkButton2");
      lnk2.Enabled=false;
    }
    

    If you want to enable/ disable Linkbutton based on value of some other column in the same row, then you can do the same inside Rowdatabound event. as:

    string Namecolumnvalue = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Name"));
    LinkButton lnk2 = (LinkButton)e.Row.FindControl("LinkButton2");
    if(Namecolumnvalue =="Disable")
    {      
      lnk2.Enabled=false;
    }
    else{
      lnk2.Enabled=true;
    }
    

提交回复
热议问题