C# 如何在gridview的命令字段控件中禁用控件
如何在gridview中查找命令字段控件 在不在行数据绑定中的方法中 到目前为止,我已经使用了这种编码,但我找不到控制C# 如何在gridview的命令字段控件中禁用控件,c#,asp.net,C#,Asp.net,如何在gridview中查找命令字段控件 在不在行数据绑定中的方法中 到目前为止,我已经使用了这种编码,但我找不到控制 <asp:CommandField ButtonType="Image" ShowEditButton="True HeaderText="Enter Leave" EditImageUrl="~/IMAGES/edit-icon.gif"> <ItemStyle Horizont
<asp:CommandField ButtonType="Image" ShowEditButton="True
HeaderText="Enter Leave"
EditImageUrl="~/IMAGES/edit-icon.gif">
<ItemStyle HorizontalAlign="Center" />
</asp:CommandField>
试试这个:
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
}
可以通过以下方式禁用特定列
EmployeeDetails.Columns[0].Visible = false;
希望这有帮助。试试这个:
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
}
可以通过以下方式禁用特定列
EmployeeDetails.Columns[0].Visible = false;
希望这有帮助。您没有指定行 比如:
ImageButton edit = (ImageButton)EmployeeDetails.Rows[0].Cells[0].FindControl("Image");
edit.Enabled = false;
如果要禁用包含imageButton的列,可以执行以下操作:
EmployeeDetails.Columns[0].Visible = false;
无法指定行 比如:
ImageButton edit = (ImageButton)EmployeeDetails.Rows[0].Cells[0].FindControl("Image");
edit.Enabled = false;
如果要禁用包含imageButton的列,可以执行以下操作:
EmployeeDetails.Columns[0].Visible = false;
您可以使用禁用列本身
GridView1.AutoGenerateEditButton = false;
从代码隐藏页面
或者您可以使用ItemTemplate而不是CommandField
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" ID="id" CommandName="Edit" Text="Edit" />
</ItemTemplate>
</asp:TemplateField>
第一次编辑: 我尝试了我的第二个解决方案,它奏效了。但是,在使用
foreach
之前,请确保您的GridView
已填充。否则,GridView.Rows.Count可能为0
第二次编辑:
这也适用于CommandField
。将0替换为GridView
中CommandField
的位置
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Enabled = false;
}
}
您可以使用禁用列本身
GridView1.AutoGenerateEditButton = false;
从代码隐藏页面
或者您可以使用ItemTemplate而不是CommandField
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" ID="id" CommandName="Edit" Text="Edit" />
</ItemTemplate>
</asp:TemplateField>
第一次编辑:
我尝试了我的第二个解决方案,它奏效了。但是,在使用foreach
之前,请确保您的GridView
已填充。否则,GridView.Rows.Count可能为0
第二次编辑:
这也适用于CommandField
。将0替换为GridView
中CommandField
的位置
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Enabled = false;
}
}
我也有类似的问题。我只是在BindData()
函数中禁用了列的视图
GridView1.Columns[0].Visible = false;
这对我很有用,因为我的第一列是编辑列,我必须只为特定用户启用它
祝你好运 我也有类似的问题。我只是在BindData()
函数中禁用了列的视图
GridView1.Columns[0].Visible = false;
这对我很有用,因为我的第一列是编辑列,我必须只为特定用户启用它
祝你好运 将其强制转换为DataControlFieldCell,然后将Enabled设置为false
其中:row.Controls[0]是您的CommandField控件
foreach (GridViewRow row in ManageDNXGridView.Rows)
{
DataControlFieldCell editable = (DataControlFieldCell)row.Controls[0];
editable.Enabled = false;
}
将其强制转换为DataControlFieldCell,然后将Enabled设置为false
其中:row.Controls[0]是您的CommandField控件
foreach (GridViewRow row in ManageDNXGridView.Rows)
{
DataControlFieldCell editable = (DataControlFieldCell)row.Controls[0];
editable.Enabled = false;
}
您应该指定要禁用imageButton的行。是否确实需要使用CommandField或ItemTemplate?如果是,我可以回答。ya命令无效。我想根据条件禁用列中的按钮您应该在哪一行指定要禁用imageButton。您真的需要使用CommandField或ItemTemplate吗?如果是,我可以回答。ya命令无效。我只想根据条件禁用列中的按钮。他不想在任何GridView事件上这样做。我想禁用该列及其自己的EmployeeDetails。列[0]。已启用此代码将完全不起作用。您是否注意到已启用的属性将完全不出现?他不希望在任何GridView事件上执行此操作。我希望将该列及其自己的EmployeeDetails禁用。列[0]。已启用此代码将不起作用。您是否注意到已启用的属性将不出现?我要禁用不可见?我需要禁用特定列?不在网格视图行中event@KAJAH因此,您应该禁用DataBoundk上该列中的所有控件,并根据条件coulmns i发送行中的代码Databound it self想要隐藏不可见我需要隐藏的特定列不在网格视图行中event@KAJAH因此,您应该禁用DataBoundk上该列中的所有控件发送行中的代码Databound it self基于条件我不需要可见我需要禁用您的意思是按钮应该可见但不应该可单击吗?我没有我想是的。我搜索了很多,但找不到命令字段的解决方案。如果要在命令字段中执行此操作,可能需要使用DataBound或RowDataBound事件。k告诉我如何处理它工作的特定列的行数据绑定:)我正在查找它。。谢谢@emrenevayeshirazi!!我不需要可见我需要禁用你的意思是按钮应该可见,但不应该是可点击的吗?我不这么认为。我搜索了很多,但找不到命令字段的解决方案。如果要在命令字段中执行此操作,可能需要使用DataBound或RowDataBound事件。k告诉我如何处理它工作的特定列的行数据绑定:)我正在查找它。。谢谢@emrenevayeshirazi!!