按另一个下拉列表ASP.NET中的值填充Dropdownlist

时间:2012-08-01 20:07:45

标签: .net sql cascadingdropdown html-select

根据另一个下拉列表中定义的值,在向databind输入下拉列表时遇到了一些麻烦。

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

那么做这种任务的最佳方式是什么?

1 个答案:

答案 0 :(得分:0)

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