根据以前的下拉列表ID过滤下拉列表

时间:2013-07-28 20:05:09

标签: c# asp.net drop-down-menu

我有三个下拉列表 - 第二个取决于第一个,第三个取决于第二个。我有两个不同的sql语句。第一个是获取第一个下拉列表的数据,第二个和第三个下拉列表取决于第二个sql语句。

我对如何使用sql语句配置下拉列表并根据id正确过滤有点遗憾。

我为第一个下拉列表启用了AutoPostBack

这就是我所拥有的:

<div>
            Section: <asp:DropDownList ID="FistDropDown" runat="server" DataSourceID="Sections" DataTextField="DisplayName" DataValueField="ID"></asp:DropDownList>
            <asp:SqlDataSource ID="Parent" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnection %>" SelectCommand="SELECT e.DisplayName,  e.ID , e.GUID
FROM Elements e
INNER JOIN ATable re
ON e.ID = re.ATableID
AND re.InstitutionsID = 1"></asp:SqlDataSource>
        </div>
        <br />
        <div style="margin-left: 65px">
            <asp:DropDownList ID="SecondDropDown" runat="server" AutoPostBack="True" DataSourceID="FirstChild" DataTextField="DisplayName" DataValueField="ID"></asp:DropDownList>
            <asp:SqlDataSource ID="FirstChild" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnection %>" SelectCommand="SELECT e.DisplayName,  e.ID , e.GUID
FROM Elements e
INNER JOIN ATableMap em
ON e.ID = em.KnowsATableID
WHERE em.ATableID = "></asp:SqlDataSource>
        </div>
        <br />
        <div style="margin-left: 75px">
             <asp:DropDownList ID="ThirdDropDown" runat="server"></asp:DropDownList>
        </div>

2 个答案:

答案 0 :(得分:0)

似乎您期望同一页面上的数据。 你必须首先输入第一个下拉列表中的值然后你可以做其余的尝试同样的第二个drplist

答案 1 :(得分:0)

您需要在SqlDataSource中使用“SelectParameters”并将其与“ControlParameter”结合使用,以将第一个下拉列表的值连接到第二个查询中的值。这将是:

<asp:SqlDataSource ID="FirstChild" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnection %>" 
    SelectCommand="SELECT e.DisplayName,  e.ID , e.GUID
                   FROM Elements e
                   INNER JOIN ATableMap em
                   ON e.ID = em.KnowsATableID
                   WHERE em.ATableID = ?">
    <SelectParameters>
        <asp:ControlParameter ControlID="FirstDropDown" PropertyName="SelectedValue" Name="ID" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>

在第一个下拉列表中处理SelectedIndexChanged事件并调用SecondDropDown.DataBind()以使查询执行并填充下拉列表。

相关问题