別のドロップダウンリストで定義された値に従って、ドロップダウンリストをデータバインドするのに問題がありました。
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
また、datasourceID で接続された asp:dropdownlist で単純なデータバインドを使用してみました
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">
では、この種のタスクを実行するための最良の方法は何ですか?