如何在GridView的命令字段控件中禁用控件 [英] How to disable a control in command field control in gridview
问题描述
如何在gridview中找到命令字段控件.
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.
<asp:CommandField ButtonType="Image" ShowEditButton="True
HeaderText="Enter Leave"
EditImageUrl="~/IMAGES/edit-icon.gif">
<ItemStyle HorizontalAlign="Center" />
</asp:CommandField>
源代码:
ImageButton edit = (ImageButton)EmployeeDetails.FindControl("Image");
edit.Enabled = false;
推荐答案
您可以通过以下方式禁用列本身,
You can disable column itself with,
GridView1.AutoGenerateEditButton = false;
来自页面后面的代码.
或者您可以使用ItemTemplate代替CommandField,
Or you can use ItemTemplate instead of CommandField,
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" ID="id" CommandName="Edit" Text="Edit" />
</ItemTemplate>
</asp:TemplateField>
在后面的代码中,您可以遍历GridView的行并禁用每个LinkButton.
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;
}
首次
我尝试了第二种解决方案,并且有效.但是,在使用 foreach
之前,请确保已填充 GridView
.否则, GridView.Rows.Count
可能为0.
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.
第二次修改:
这也适用于 CommandField
.在您的 GridView
中,将 CommandField
的位置替换为0.
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;
}
}
这篇关于如何在GridView的命令字段控件中禁用控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!