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 下拉列表


- C#MongoDB使用Builders查找派生对象 2022-09-04
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01