使用 DataSet 设计器创建的 DataSet 中的 DataSet 数据填充 ASP.Net 下拉列表数据、列表、DataSet、Net

2023-09-07 08:40:47 作者:爱咬吸管的女孩子

我们有一个 ASP.Net/VB.Net web 表单,其中包含 aspx 文件标记中的下拉列表.还有一个使用 DataSet 设计器创建的 DataSet.

我们想用 DataSet 中的数据填充下拉列表.

使用VS2005DataSet设计器实现数据访问层 weixin 30293135的博客 CSDN博客

您能否向我展示一些使用 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();
}