我想为自己创建radiobuttonlist的多个项目,该项目具有文本和值属性。如何在C#/ asp.net中做到这一点?提前致谢。如何为radiobuttonlist创建数据源?
10
A
回答
14
你能给我们一个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.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"));
1
您可以使用DataTable作为数据源(或其他可绑定源),并将DataTable绑定到RadioButton列表。使用DataTextField和DataValueField属性指定哪个列用于文本,哪些用于该值。
1
您可以创建自己的数据源将与其他标准控件一起被自动显示在VisualStudio的工具箱,如果你真的需要这种类型的数据源的尝试以下操作:
public class CustomDataSource : System.Web.UI.WebControls.ObjectDataSource
{
public CustomDataSource()
{
// Hook up the ObjectCreating event so we can use our custom object
ObjectCreating += delegate(object sender, ObjectDataSourceEventArgs e)
{
// Here we create our custom object that the ObjectDataSource will use
e.ObjectInstance = new DataAccessor()
};
}
class DataAccessor
{
[DataObjectMethod(DataObjectMethodType.Insert, true)]
public void Add(string text, string value)
{
// Insert logic
}
[DataObjectMethod(DataObjectMethodType.Update, true)]
public void Update(int id, string text, string value)
{
// Update logic
}
[DataObjectMethod(DataObjectMethodType.Select, true)]
public IEnumerable<MyRadioButtonEntryWrapper> List(int filterById)
{
// Select logic
}
}
ASPX :
<%@ Register TagPrefix="cc1" Namespace="DataLayer.DataSources" %>
<cc1:CustomDataSource ID="customDataSource" runat="server"
TypeName="DataAccessor"
OldValuesParameterFormatString="original_{0}"
InsertMethod="Add"
UpdateMethod="Update">
<UpdateParameters>
<asp:Parameter Name="id" Type="Int32" />
<asp:Parameter Name="text" Type="String" />
<asp:Parameter Name="value" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="text" Type="String" />
<asp:Parameter Name="value" Type="String" />
</InsertParameters>
</cc1:ArticleTypeDataSource>
相关问题
- 1. TTPickerTextField数据源?我如何创建我自己的数据源?
- 2. 在Java中为MongoDB创建数据源
- 3. 如何使用JSON作为数据源创建子组?
- 4. 如何使用IterationPath作为数据源创建新字段
- 5. 如何为集合视图创建多个数据源?
- 6. 如何创建同步到数据源/数据库
- 7. 如何为gnuplot创建数据文件?
- 8. 如何为数据库创建mongodb oplog?
- 9. 如何为Android创建数据网格?
- 10. 如何为船创建数据模型?
- 11. 从数据库创建新闻源作为源
- 12. 如何为站点创建新资源
- 13. EXTJS,如何使用由PHP创建的JSON数据为XTemplate创建数据
- 14. 如何创建JSON数据
- 15. 如何创建数据框?
- 16. 如何创建数据库
- 17. 如何创建base64Binary数据?
- 18. 如何在asp.net mvc 3中创建一个强类型的radiobuttonlist?
- 19. 如何创建资源DLL
- 20. 从JDBC创建数据源URL
- 21. 使用数据源创建PDF
- 22. 使用HDInsight创建外部数据源
- 23. 创建Wildfly 10数据源的问题
- 24. 创建自己的数据源
- 25. 需要创建一个XML数据源
- 26. 创建XA数据源到DB2在JBoss
- 27. 从eclipse创建DatabaseConnection数据源
- 28. 错误时创建数据源
- 29. 创建数据源的功能
- 30. 在Application.cfc中创建数据源