使用SQL查询更新dropDownList

时间:2014-02-16 18:14:35

标签: asp.net sql

我正在尝试根据dropdownlistA中选择的categoryId更新我的dropdownlistB使用此代码:

<asp:DropDownList ID="DropDownListA" runat="server" DataSourceID="SqlDataSourceA" DataTextField="Description" DataValueField="Description" AutoPostBack="True"></asp:DropDownList>
<asp:DropDownList ID="DropDownListB" runat="server" DataSourceID="SqlDataSourceB" DataTextField="Title" DataValueField="Title"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceA" runat="server" ConnectionString="<%$ ConnectionStrings:MainDbConnectionString1 %>" SelectCommand="SELECT [Description] FROM [BookCategory]"></asp:SqlDataSource>
 <asp:SqlDataSource ID="SqlDataSourceB" runat="server" ConnectionString="<%$ ConnectionStrings:MainDbConnectionString1 %>" SelectCommand="SELECT [Title] FROM [BooksInfo] WHERE ([CategoryId] = @CId)">
     <SelectParameters>
         <asp:QueryStringParameter DefaultValue="" Name="CId" QueryStringField="SELECT [CategoryId] FROM [BookCategory]" Type="Int32" />
     </SelectParameters>
</asp:SqlDataSource>

我不熟悉在ASP.NET中使用SQL和查询,并且无法弄清楚我做错了什么,dropdownlistB保持空白。 (DropdownlistA中的AutoPostBack = true,所以它应该更新?)

1 个答案:

答案 0 :(得分:1)

我认为您正在寻找的是asp:ControlParameter这样的

<asp:ControlParameter ControlID="DropDownListA" PropertyName="SelectedValue"
                                  Name="EmpID" Type="Int32" DefaultValue="0" />

因此查询基于 DropDownListA 的选择。