Populating an ASP.Net Drop Down List with DataSet data from DataSet created with DataSet designer(使用 DataSet 设计器创建的 DataSet 中的 DataSet 数据填充 ASP.Net 下拉列表)
问题描述
我们有一个 ASP.Net/VB.Net web 表单,其中包含 aspx 文件标记中的下拉列表.还有一个使用 DataSet 设计器创建的 DataSet.
我们想用 DataSet 中的数据填充下拉列表.
您能否向我展示一些使用 DataSet 数据填充下拉列表所需的示例标记和/或 VB.Net 编码?
<asp:DropDownList ID="MyDropDownList" runat="server" DataTextField="SomeString" DataValueField="SomeUniqueId"/>代码隐藏:
protected void Page_Load(object sender, EventArgs e){var myDataSet = new DataSet();//替换为您的数据集MyDropDownList.DataSource = myDataSet;MyDropDownList.DataBind();}VB.Net:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) 处理 Me.Load将 aDataSet 调暗为 DataSetMyDropDownList.DataSource = 一个数据集MyDropDownList.DataBind()结束子您在标记中看到DataTextField"和DataValueField"了吗?您可以在其中放置要用作 id(数据值)并在下拉列表中显示(数据文本)的字段名称.
这是一个例子:
标记
<form id="form1" runat="server">水果<asp:DropDownList ID="DropDownListWithFruits" runat="server" DataTextField="FruitName" DataValueField="FruitId"/></div></表格></身体>后面的代码
protected void Page_Load(object sender, EventArgs e){var myDataSet = new DataSet();var table1 = new DataTable();table1.Columns.Add("水果名称");table1.Columns.Add("FruitId");table1.Rows.Add("苹果", 1);table1.Rows.Add("香蕉", 2);table1.Rows.Add("葡萄柚", 3);myDataSet.Tables.Add(table1);DropDownListWithFruits.DataSource = myDataSet;DropDownListWithFruits.DataBind();}
We have an ASP.Net / VB.Net web form containing a drop down list in the markup of the aspx file. There is also a DataSet created with the DataSet designer.
We would like to populate the drop down with data from the DataSet.
Can you show me some sample markup and / or VB.Net coding that is required to populate the drop down list with the DataSet data?
解决方案 <asp:DropDownList ID="MyDropDownList" runat="server" DataTextField="SomeString" DataValueField="SomeUniqueId" />
Code-behind:
protected void Page_Load(object sender, EventArgs e)
{
var myDataSet = new DataSet(); // replace with your dataset
MyDropDownList.DataSource = myDataSet;
MyDropDownList.DataBind();
}
VB.Net:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim aDataSet As DataSet
MyDropDownList.DataSource = aDataSet
MyDropDownList.DataBind()
End Sub
And you see the "DataTextField" and "DataValueField" in the markup? There's where you put the name of fields you want to use as id (data value) and to display (data text) in the drop-down list.
Here's an example:
Markup
<body>
<form id="form1" runat="server">
<div>
Fruits
<asp:DropDownList ID="DropDownListWithFruits" runat="server" DataTextField="FruitName" DataValueField="FruitId" />
</div>
</form>
</body>
Code behind
protected void Page_Load(object sender, EventArgs e)
{
var myDataSet = new DataSet();
var table1 = new DataTable();
table1.Columns.Add("FruitName");
table1.Columns.Add("FruitId");
table1.Rows.Add("Apple", 1);
table1.Rows.Add("Banana", 2);
table1.Rows.Add("Grapefruit", 3);
myDataSet.Tables.Add(table1);
DropDownListWithFruits.DataSource = myDataSet;
DropDownListWithFruits.DataBind();
}
这篇关于使用 DataSet 设计器创建的 DataSet 中的 DataSet 数据填充 ASP.Net 下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 DataSet 设计器创建的 DataSet 中的 DataSet 数据填充 ASP.Net 下拉列表
基础教程推荐
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
