[英]How to disable a control in command field control in gridview
how to find a command field control in the gridview. 如何在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>
source code: 源代码:
ImageButton edit = (ImageButton)EmployeeDetails.FindControl("Image");
edit.Enabled = false;
You can disable column itself with, 您可以使用以下方式禁用列本身:
GridView1.AutoGenerateEditButton = false;
from code behind pages. 从页面后面的代码。
Or you can use ItemTemplate instead of CommandField, 或者您可以使用ItemTemplate代替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. 在后面的代码中,您可以遍历GridView的行并禁用每个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
. 但是,在使用
foreach
之前,请确保已填充GridView
。 Otherwise, GridView.Rows.Count
would probably be 0. 否则,
GridView.Rows.Count
可能为0。
Second Edit : 第二次编辑:
This works for CommandField
too. 这也适用于
CommandField
。 Replace 0 with the location of CommandField
in your GridView
. 将0替换为
GridView
中CommandField
的位置。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Enabled = false;
}
}
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. 希望这可以帮助。
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 : 如果要禁用包含imageButton的列,则可以执行以下操作:
EmployeeDetails.Columns[0].Visible = false;
I had a similar issue. 我有一个类似的问题。 I simply disabled the view of the Column in
BindData()
function. 我只是在
BindData()
函数中禁用了列的视图 。
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! 祝好运!
Cast it as a DataControlFieldCell and then set Enabled to false. 将其强制转换为DataControlFieldCell,然后将Enabled设置为false。
Where: row.Controls[0] is your CommandField control 其中:row.Controls [0]是您的CommandField控件
foreach (GridViewRow row in ManageDNXGridView.Rows)
{
DataControlFieldCell editable = (DataControlFieldCell)row.Controls[0];
editable.Enabled = false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.