使用 DataSet 设计器创建的 DataSet 中的 DataSet 数据填充 ASP.Net 下拉列表 [英] Populating an ASP.Net Drop Down List with DataSet data from DataSet created with DataSet designer

查看:22
本文介绍了使用 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 下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆