如何创建单选按钮列表的数据源? [英] how to create datasource for radiobuttonlist?
本文介绍了如何创建单选按钮列表的数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想自己为单选按钮列表创建几个项目,该项目具有text和value属性.如何在c#/asp.net中做到这一点?预先感谢.
I want to create several items for radiobuttonlist by myself, the item has text and value properties. How to do it in c#/asp.net? Thanks in advance.
推荐答案
您可以使用Dictionary对象来存储键/值,并将其绑定到RadioButtonList上,如下所示:
You can us a Dictionary object to store the key/values and bind it to the RadioButtonList like so:
Dictionary<string, string> values = new Dictionary<string, string>();
values.Add("key 1", "value 1");
values.Add("key 2", "value 2");
values.Add("key 3", "value 3");
RadioButtonList radioButtonList = new RadioButtonList();
radioButtonList.DataTextField = "Value";
radioButtonList.DataValueField = "Key";
radioButtonList.DataSource = values;
radioButtonList.DataBind();
或者您也可以将项目添加到RadioButtonList Items集合中,如下所示:
Or you can also add the items to the RadioButtonList Items collection like so:
radioButtonList.Items.Add(new ListItem("Text 1", "Value 1"));
radioButtonList.Items.Add(new ListItem("Text 2", "Value 2"));
radioButtonList.Items.Add(new ListItem("Text 3", "Value 3"));
这篇关于如何创建单选按钮列表的数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文