在ASP:Repeater中连接2个dropDownLists和SqlDataSource FilterExpression

时间:2013-01-21 11:27:24

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

在转发器行中使用2个dropDownLists如何使用第一个作为第二个过滤器?

转发器布局很简单:[category_dropDown][item_dropDown][add_button]

问题是我无法连接两个dropDown控件。 SqlDataSource ControlParameter找不到要调用的ControlID(由repeater重命名的控件)。将ControlID值更改为“itemRepeater $ dropDownCategory”显然没有帮助。如何绑定这些dropDowns成对工作?

主要想知道是否存在标记代码解决方案,因为代码隐藏解决方案将更容易实现。

<asp:Repeater ID="itemRepeater" runat="server" OnItemCommand="itemRepeater_ItemCommand" onitemdatabound="itemRepeater_ItemDataBound">
    <HeaderTemplate>
        <table>
        <tr>
            <td>Category</td>
            <td>Item</td>
            <td></td>
        </tr>
    </HeaderTemplate>
    <ItemTemplate>        
        <tr>
            <td><asp:DropDownList ID="dropDownCategory" runat="server" DataSourceID="SqlDataSourceCategory" DataTextField="Category" 
                DataValueField="ID_cat" SelectedValue='<%# DataBinder.Eval(Container.DataItem,"Category") %>' AppendDataBoundItems="true">                    
                <asp:ListItem Value="%" Text="Pick category" Selected="True" />
                </asp:DropDownList></td>
            <td><asp:DropDownList ID="dropDownItem" runat="server" DataSourceID="SqlDataSourceItem" DataTextField="Item" 
                DataValueField="ID_item" SelectedValue='<%# DataBinder.Eval(Container.DataItem,"Item") %>' AppendDataBoundItems="true">
                <asp:ListItem Value="%" Text="Pick item" Selected="True"  />
                </asp:DropDownList></td>   
            <td><asp:Button ID="repeatedButton" runat="server" CommandName='<%# DataBinder.Eval(Container.DataItem, "Button") %>' Text='<%# DataBinder.Eval(Container.DataItem, "Button") %>' /></td>             
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>


<asp:SqlDataSource ID="SqlDataSourceCategory" runat="server" ConnectionString="..." 
    SelectCommand="SELECT [Category], [ID_cat] FROM [Categories]">
</asp:SqlDataSource>

<asp:SqlDataSource ID="SqlDataSourceItems" runat="server" ConnectionString="..." 
    SelectCommand="SELECT [ID_item],[Item] FROM [Items]"  FilterExpression="WHERE [ID_cat] = @ID_cat" >
    <FilterParameters>
        <asp:ControlParameter Name="ID_cat" ControlID="dropDownCategory" PropertyName="SelectedValue" />
    </FilterParameters>
</asp:SqlDataSource>    

上面的ControlID绑定显然是错误的,因为SqlDataSourceItems在ItemTemplate中找不到名为“dropDownCategory”的ControlID并抛出已知异常:无法在ControlParameter'ID_cat'中找到控件'dropDownCategory'

提前感谢任何建议。

1 个答案:

答案 0 :(得分:1)

您好以下链接将为您提供诀窍:

Solution 1

Solution 2

例如:像这样:

 DropDownList ddlcity = (DropDownList)grow.FindControl("ddlcity"); // To get the value of dropdown

        cmd.Parameters.Add("@city", ddlcity.SelectedItem.ToString());  // This will add selected item to database