How to disable a control in command field control in gridview

后端 未结 5 449
刺人心
刺人心 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,

    <asp:TemplateField>
        <ItemTemplate>
            <asp:LinkButton runat="server" ID="id" CommandName="Edit" Text="Edit" />
        </ItemTemplate>
    </asp:TemplateField>
    

    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;
        }
    }
    
    0 讨论(0)
  • 2021-01-07 09:49

    You miss to specify the row

    Something like :

    ImageButton edit = (ImageButton)EmployeeDetails.Rows[0].Cells[0].FindControl("Image");
    edit.Enabled = false;
    

    If you want to disable the column that contains the imageButton , you can do :

    EmployeeDetails.Columns[0].Visible = false;
    
    0 讨论(0)
  • 2021-01-07 09:51

    I had a similar issue. I simply disabled the view of the Column in BindData() function.

    GridView1.Columns[0].Visible = false;
    

    This worked for me, since my first column was Edit column and I have to enable it for specific users only.

    Good luck!

    0 讨论(0)
  • 2021-01-07 09:53

    Cast it as a DataControlFieldCell and then set Enabled to false.

    Where: row.Controls[0] is your CommandField control

    foreach (GridViewRow row in ManageDNXGridView.Rows)
    {
        DataControlFieldCell editable = (DataControlFieldCell)row.Controls[0];
        editable.Enabled = false; 
    }
    
    0 讨论(0)
  • 2021-01-07 10:10

    Try this:

    try to hide controls at DataBound or RowDataBound event of GridView
    
    protected void EmployeeDetails_DataBound(object sender, EventArgs e)
    {
            ImageButton edit = (ImageButton)EmployeeDetails.Row.Cells[0].FindControl("Image");
            edit.Visible = false;  
            edit.Enabled = false; //OR use this line
    }
    

    particular column can be disabled in the following way

    EmployeeDetails.Columns[0].Visible = false;
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题