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# Math.Sqrt 实现 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 XML 转换为通用列表 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01