问题描述
我试过的代码如下
我在gridview rowdatabound中编写代码
My tried code as follows
I am writing code in gridview rowdatabound
protected void gvPkgcbndate_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (((Button)e.Row.FindControl("select")).Text == "click Here")
{
ButtonField btnclick = (Button)e.Row.Cells[0].Controls[0];
btnclick.Visible = false;
}
}
我的源页面代码如下
My source page code as follows
<asp:ButtonField Text="Click Here" CommandName="Select"/>
在gridview中的运行模式如下
In run mode in gridview as follows
click here 27 Aug 2015
click here 30 Aug 2015
click here 31 Aug 2015
click here
click here 30 Aug 2015
click here 31 Aug 2015
i想要禁用第4行点击这里按钮。
i想禁用它网格视图的空行单击此处按钮。
当我运行时显示错误如下
i want to disable 4th row click here button.
i want to disable the empty row of gridview click here button.
When i run shows error as follows
Cannot implicitly convert type 'System.Web.UI.WebControls.Button' to 'System.Web.UI.WebControls.ButtonField'
请帮我解决上面的问题代码
please help me what is the problem in my above code
推荐答案
ButtonField btnclick = (Button)e.Row.Cells[0].Controls[0];
您始终可以将ButtonFields表示为按钮(向下转换),但您只能将Downcast ButtonFields表示为ButtonFields,而不是普通按钮。
所以:所有ButtonField都是按钮但不是所有按钮都是ButtonFields。
编译器信任你的时候你这样做一个上流。
一个更安全的演员方式是这样的:
You can always represent ButtonFields as Buttons (downcast), but you can only represent downcast ButtonFields as ButtonFields, not plain Buttons.
So: All ButtonFields are Buttons but not all Buttons are ButtonFields.
The compiler trusts you when you perform an upcast like this.
A safer way to cast is like this:
ButtonField btnclick = e.Row.Cells[0].Controls[0] as Button;
if(btnclick==null)
//cast did not work!
无论如何 - 回到你的问题。尝试将控件用作按钮:
Anyway - back to your problem. Try just using the control as a button:
Button btnclick = e.Row.Cells[0].Controls[0] as Button;
if(btnclick==null)
//cast did not work!
或尝试投射而不是向上投射:
or try casting instead of upcasting:
ButtonField btnclick = e.Row.Cells[0].Controls[0] as ButtonField;
if(btnclick==null)
//cast did not work!
希望有帮助:)
Hope that helps :)
这篇关于如何在c#中禁用特定条件下的按钮字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!