【问题标题】:Populate a Dropdownlist by a value in another dropdownlist ASP.NET用另一个下拉列表 ASP.NET 中的值填充下拉列表
【发布时间】:2012-08-01 20:07:45
【问题描述】:

根据另一个下拉列表中定义的值将下拉列表进行数据绑定时遇到了一些问题。

Protected Sub CategoryDDL_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CategoryDDL.SelectedIndexChanged
    qaDDL.Items.Clear()
    qaDDL.Items.Insert(0, New ListItem("(New Question)"))

    If CategoryDDL.SelectedValue = "(New Category)" Then
        CategoryName.Text = Nothing
    Else
        Dim filter As String = "categoryID = '" + CategoryDDL.SelectedValue.ToString() + "'"
        Dim dv As DataView = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
        dv.RowFilter = filter
        For Each drv As DataRowView In dv
            CategoryName.Text = drv("Name").ToString()
        Next

我已经尝试过类似于填充上述文本框的方法,但我不知道如何将其用于数组。

    dv = CType(SqlDataSource2.Select(DataSourceSelectArguments.Empty), DataView)
        For Each drv As DataRowView In dv
            qaDDL.Items.Insert(drv("placement").ToString(), drv("Question").ToString())
        Next
    End If
End Sub

我也尝试过使用简单的数据绑定和 asp:dropdownlist 通过数据源ID 连接

qaDDL.Databind()

这是页面的样子。

<asp:DropDownList runat="server" ID="CategoryDDL" DataSourceID="SqlDataSource1" Width="300px" DataTextField="Name" DataValueField="categoryID" AppendDataBoundItems="True" AutoPostBack="True" >
            <asp:ListItem Value="(New Category)">(New Category)</asp:ListItem>
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings%>" SelectCommand="SELECT categoryID, Name, placement FROM FAQ_category ORDER BY placement"></asp:SqlDataSource>

<asp:DropDownList runat="server" ID="qaDDL" Width="300px" AppendDataBoundItems="True" AutoPostBack="True" DataSourceID="SqlDataSource2" DataTextField="Question" DataValueField="faqID">
            <asp:ListItem Value="(New Question)">(New Question)</asp:ListItem>
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings%>" SelectCommand="SELECT faqID, categoryID, Question, Answer FROM FAQ WHERE (categoryID = @categoryID) ORDER BY placement">

那么完成此类任务的最佳方法是什么?

【问题讨论】:

  • 我认为html.dropdownlist的标签可能不正确

标签: .net sql cascadingdropdown html-select


【解决方案1】:

最好的方法可能是主观的,但我建议最简单的方法是使用 AjaxControlToolkit 的 CascadingDropDownList 扩展器控件,因为它可以为您节省回发,而且很多工作已经完成为你完成。

【讨论】:

  • 它是否适用于 SQL 数据库?我认为它可以使用 XML。
  • @Quinson 网站上有一个walkthrough 用于将其连接到 SQL 数据库。我正在为你编写一些演示代码,不过今晚我可以完成它之前...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-13
  • 1970-01-01
  • 2018-02-03
  • 1970-01-01
  • 1970-01-01
  • 2020-11-11
  • 1970-01-01
相关资源
最近更新 更多