Connecting 2 dropDownLists with SqlDataSource FilterExpression inside ASP:Repeater

↘锁芯ラ 提交于 2019-12-24 22:44:59

问题


Using 2 dropDownLists in repeater row how does one use the 1st one as a filter for 2nd?

The repeater layout is simple: [category_dropDown][item_dropDown][add_button]

The problem is that I can't connect two dropDown controls. SqlDataSource ControlParameter cannot find ControlID to call (controls renamed by repeater). Changing ControlID value to "itemRepeater$dropDownCategory" obviously doesn't help. How do I bind these dropDowns to work in pairs?

Wondering mostly if there is a markup code solution, since code-behind solution will be easier to implement.

<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>    

The ControlID binding above is obviously wrong, because SqlDataSourceItems can't find ControlID called "dropDownCategory" in ItemTemplate and throws known exception: Could not find control 'dropDownCategory' in ControlParameter 'ID_cat'

Thanks in advance for any advice.


回答1:


Hi following link will give you the trick:

Solution 1

Solution 2

e.g: Something like this:

 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


来源:https://stackoverflow.com/questions/14437635/connecting-2-dropdownlists-with-sqldatasource-filterexpression-inside-asprepeat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!