查找下拉列表中的异常 [英] exception in find dropdown

查看:86
本文介绍了查找下拉列表中的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

postgdetails.Qua = ((DropDownList)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).SelectedItem.ToString();


其中postgdetails是类的对象.
调试此代码时,它将在此行中引发异常.异常在下面给出


where postgdetails is a object of a class.
when debug this code it throw a exception in this line .exception are given below

Unable to cast object of type ''System.Web.UI.LiteralControl'' to type ''System.Web.UI.WebControls.DropDownList''.






while

postgdetails.Medium = ((DropDownList)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).SelectedItem.Text.ToString();


这条线在同一程序中正常工作.

请告诉我有关该问题的信息.


this line is working properly in same program how is that.

plz tell me about that problem.

推荐答案

不同单元格的单元格内容的运行时类型不同.一种类型是DropDownList或与它兼容的分配,另一种类型不是.当您动态将其向下转换为不兼容的类型时,它将引发此异常.检查相关单元格中的实际内容.就这么简单.

通常,最好检查运行时类型而不是盲目折叠类型转换.像这样的东西:
The run-time types of the cell contents are different for the different cells. One type is DropDownList or assignment-compatible with it, another type is not. When you dynamically down-cast it to an incompatible type, it throws this exception. Check up what is actually in the cell in question. As simple as that.

Generally, it can be good to check up a run-time type instead of blind-folded type casting. Something like that:
DropDownList list = GridView1.Rows[e.RowIndex].Cells[1].Controls[0] as DropDownList;
if (list != null) // successful cast
   postgdetails.Qua = list.SelectedItem.ToString();
else
   MessageBox.Show("Oh no! Not again! :-)");


—SA


这篇关于查找下拉列表中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆