如何在gridview中禁用命令字段控件中的控件

Kaj*_*ser 1 c# asp.net

如何在gridview中查找命令字段控件.

在不在行数据绑定中的方法中.

到目前为止,我已经使用了这个编码,但我找不到控件.

<asp:CommandField ButtonType="Image" ShowEditButton="True
                  HeaderText="Enter Leave"
                  EditImageUrl="~/IMAGES/edit-icon.gif">
    <ItemStyle HorizontalAlign="Center" />
</asp:CommandField>
Run Code Online (Sandbox Code Playgroud)

源代码:

ImageButton edit = (ImageButton)EmployeeDetails.FindControl("Image");
edit.Enabled = false;
Run Code Online (Sandbox Code Playgroud)

emr*_*azi 11

你可以用,禁用列本身,

GridView1.AutoGenerateEditButton = false;
Run Code Online (Sandbox Code Playgroud)

来自页面后面的代码.


或者您可以使用ItemTemplate而不是CommandField,

<asp:TemplateField>
    <ItemTemplate>
        <asp:LinkButton runat="server" ID="id" CommandName="Edit" Text="Edit" />
    </ItemTemplate>
</asp:TemplateField>
Run Code Online (Sandbox Code Playgroud)

在代码后面,您可以遍历GridView行并禁用每个LinkBut​​ton.

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

第一编辑:

我尝试了我的第二个解决方案并且有效.但是,请确保GridView在使用前填满foreach.否则,GridView.Rows.Count可能是0.


第二编辑:

这也适用CommandField.将0替换为CommandField您的位置GridView.

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