【问题标题】:get the value in ListItem获取 ListItem 中的值
【发布时间】:2015-06-18 09:28:51
【问题描述】:

我试图从下拉列表中的所有 ListItems 中获取值,但是当我将它们视为控件时,我得到一个错误:

无法将类型“system.web.ui.control”转换为“system.web.ui.webcontrols.listitem”

我的代码:

if (c.GetType() == typeof(ListItem))
 string id = c.ID;
 string value = ((ListItem)(c)).Value; ///I get the error here (ListItem)(c)

如何获得这些值?我想在不知道这些元素的 ID 的情况下这样做。

【问题讨论】:

  • c ListItem 还是 DropDownList?
  • c 是控制foreach (Control c in ctl.Controls) { .... }
  • ctl 你的下拉菜单吗?

标签: c# asp.net webforms controls listitem


【解决方案1】:

你可以试试这个吗?

foreach (var item in ctl.Items) 
{
    var value = item.Value;
} 

【讨论】:

  • 我试过了,但我在 ((System.Web.UI.Webcontrols.ListItem)(c)) 得到了represents a data item in a data-bound list control. This class can not get inherited
  • 我猜你正在尝试这样做?
  • 抱歉,我的回答延迟了,我没有解决这个问题。我已经尝试过您的“ctl.Items 中的 var 项目”,但我收到一个错误,即 Syste.web.UI.Control 不包含项目的定义。
  • 已解决... ctl 不是下拉列表控件,这就是我不能使用 ctl.Items 的原因。但我可以使用您的信息来解决问题。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多