解决方法
我们可以使用一个Dictionary对象来存储键值,并将其绑定到RadioButtonList,如下所示:
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集合中,如下所示:
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"));