【问题标题】:Connecting 2 dropDownLists with SqlDataSource FilterExpression inside ASP:Repeater在 ASP:Repeater 中使用 SqlDataSource FilterExpression 连接 2 个 dropDownLists
【发布时间】:2013-01-21 11:27:24
【问题描述】:

在中继器行中使用 2 个 dropDownLists 如何将第一个用作第二个的过滤器?

中继器布局很简单:[category_dropDown][item_dropDown][add_button]

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

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

<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并抛出已知异常:Could not find control 'dropDownCategory' in ControlParameter 'ID_cat'

提前感谢您的任何建议。

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

    • 谢谢。它很有用,但仍然无法正常工作。如果它只是静态网格上的级联下拉菜单,那没问题,但由于它们是动态添加的,我在多种情况下不断收到许多不同的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-15
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多