How to disable a control in command field control in gridview

后端 未结 5 507
刺人心
刺人心 2021-01-07 09:07

how to find a command field control in the gridview.

in a method not in the row data bound.

so far i have used this coding but i cant find the control.

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-07 09:48

    You can disable column itself with,

    GridView1.AutoGenerateEditButton = false;
    

    from code behind pages.


    Or you can use ItemTemplate instead of CommandField,

    
        
            
        
    
    

    And at code behind you can iterate through rows of GridView and disable each LinkButton.

    foreach(GridViewRow gvr in GridView1.Rows)
    {
        LinkButton row = gvr.FindControl("id") as LinkButton;
        row.Enabled = false;
    } 
    

    First Edit :

    I tried my second solution and it works. However, make sure your GridView is filled before you use foreach. Otherwise, GridView.Rows.Count would probably be 0.


    Second Edit :

    This works for CommandField too. Replace 0 with the location of CommandField in your GridView.

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {        
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
             e.Row.Cells[0].Enabled = false;
        }
    }
    

提交回复
热议问题