无法投放错误.... [英] Unable to cast error....
问题描述
GridView控件验证存在一些问题.我在GridView的所有行的第一列中都有Asp CheckBox,我需要删除通过CheckBox选择的所有行.
所以我必须检查是否使用(CheckBox)选择了该行.但是,当我在运行时捕获结果以检查其是否已检查时,我得到了一个错误.在将布尔结果捕获到(bool b)时引发错误
无法将类型为"System.Web.UI.LiteralControl"的对象转换为类型为"System.Web.UI.WebControls.CheckBox".
我使用的代码是:
Hi,
I have some problem with GridView Control validations. I have Asp CheckBox at first column in all the rows of my GridView, I need to delete all the rows which are selected through CheckBox.
So i have to check whether the row is selected or not using (CheckBox). But when i am catching the result at runtime to check whether its checked or not, i am getting an error. The error is raised at catching the boolean result to (bool b)
Unable to cast object of type ''System.Web.UI.LiteralControl'' to type ''System.Web.UI.WebControls.CheckBox''.
The code i used is:
bool b = ((CheckBox)GridView1.Rows[i].Cells[0].Controls[0]).Checked;
if(b)
{
string strid=(string)GridView1.Rows[i].Cells[1].Text;
cusidlist.Add(strid);
}
谢谢.....
Thanks.....
推荐答案
无法将类型为"System.Web.UI.LiteralControl"的对象转换为类型为"System.Web.UI.WebControls.CheckBox".
Unable to cast object of type ''System.Web.UI.LiteralControl'' to type ''System.Web.UI.WebControls.CheckBox''.
错误是不言自明的.在创建网格时,已使用文字控件显示为复选框.现在,在获取值的同时,您尝试将该控件转换为复选框.
解决方案:
只需将其转换为用于创建它的控件类型即可.
Error is self-explanatory. You have used a Literal Control to present as checkbox while creating the grid. Now, while getting the values you are trying to cast that control into checkbox.
Solution:
Just cast it into the same control-type that you used to create it.
您不能将另一个控件转换为复选框.
您需要确保GridView1.Rows[i].Cells[0].Controls[0])
是一个复选框.
You cannot convert another control to a checkbox.
You need to make sureGridView1.Rows[i].Cells[0].Controls[0])
is a checkbox.
Hai Sandeep Mewara,
您能否解释如何创建文字控件并将类型转换为相同的文字控件.我尝试了一下,但是无法在该行代码中将其转换为文字控制.您能否为此发送代码.
在此先感谢.....
Hai Sandeep Mewara,
Can u pls explain how to create a literal control and to type cast the result to the same literal control. I tried it but not able to cast to literal control at that line of code. Can u pls send code for that.
Thanks in Advance.....
这篇关于无法投放错误....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!