如何根据asp.net中另一个下拉列表的选定项目从数据库填充下拉列表

时间:2015-04-04 13:20:16

标签: c# asp.net ajaxcontroltoolkit

我有一个名为医生搜索的SQL表,有2个下拉列表,其中一个显示医生的专业知识,我想填写下拉列表2,其中包含与该专业科相关的医生信息。

1 个答案:

答案 0 :(得分:0)

如果您使用的是设计器界面,则可以轻松完成。     在进行必要的更改(连接字符串,表名和列名)后尝试使用以下代码。如果这不起作用,我将分享步骤

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Your Connection String %>" SelectCommand="SELECT [Expertise] FROM [Your Table]"></asp:SqlDataSource>

    <asp:DropDownList ID="ddlExpertise" runat="server" DataSourceID="SqlDataSource1" DataTextField="Expertise" DataValueField="Expertise">
        </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Your Connection String %>" SelectCommand="SELECT [DoctorDetails] FROM [Your Table] WHERE ([Expertise] = @Expertise)">
                <SelectParameters>
                    <asp:ControlParameter ControlID="ddlExpertise" DefaultValue="" Name="Expertise" PropertyName="SelectedValue" Type="String" />
                </SelectParameters>
            </asp:SqlDataSource>
    <asp:DropDownList ID="ddlDoctorDetails" runat="server" DataSourceID="SqlDataSource2" DataTextField="DoctorDetails" DataValueField="DoctorDetails">
            </asp:DropDownList>