c# - 将复杂类型的字典绑定(bind)到 RadioButtonList

标签 c# asp.net webforms

我不确定这是否可行,但我有一个复杂类型的字典,我想将它绑定(bind)到 RadioButtonList/CheckBoxList 控件:

public class ComplexType
{
   public String Name { get; set; }
   public String FormattedName { get; set; }
   public String Description { get; set; }
}
var listOfMyTypes = new Dictionary<int, ComplexType>();

var myType = new ComplexType { Name = "Name", Description = "Description", FormattedName = "Name|Description" };
var myType1 = new ComplexType { Name = "Name2", Description = "Description2", FormattedName = "Name|Description2" };


listOfMyTypes.Add(1, myType);
listOfMyTypes.Add(2, myType1);

m_dropDownlist.DataTextField = "Value"; //What do I put here to render "Name"
m_dropDownlist.DataValueField = "Key";
m_dropDownlist.DataSource = listOfMyTypes;
m_dropDownlist.DataBind();

最佳答案

尝试像这样绑定(bind)到 Dictionary.Values:

m_dropDownlist.DataTextField = "Name"
m_dropDownlist.DataValueField = "Description";
m_dropDownlist.DataSource = listOfMyTypes.Values;
m_dropDownlist.DataBind();

关于c# - 将复杂类型的字典绑定(bind)到 RadioButtonList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11274523/

相关文章:

c# - 如何在 asp.net 中强制文化?

c# - 如何通过链接使元素出现在屏幕中央?

javascript - 如何验证 MM/dd/yyyy hh :mm format? 的 DateTime

javascript - ASP.Net GridView JavaScript 弹出模式窗口 - 设置透明度?

asp.net - 缺少 Web API Controller 类模板 VS 2010

c# - 在 URL 字符串中的斜杠前转义句号

c# - 为什么异常重定向到不同的页面?

html - 将两个 div 合并到一个具有响应功能的 div 中

asp.net - <asp :Login> LayoutTemplate 中获取用户名文本框的值

c# - 尝试在动态生成的 html 标记上应用样式属性