0

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

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 连接的 datasourceID

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">

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

4

1 回答 1

0

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

于 2012-08-01T20:31:03.250 回答