Search code examples
asp.netrepeatersqldatasource

SqlDataSource-Query in a repeater to use repeater-elements


I would like to do the following with asp.net and C#:

An SqlDataSource fetches a list of templates from the database. Then, in a repeater, for each template a DropDownList-element is created, that contains the found types for each template. Is this possible?

<asp:SqlDataSource ID="src_template" runat="server" ConnectionString="<%$ ConnectionStrings:VConStr %>" SelectCommand="SELECT [template] FROM [template]" />
<asp:Repeater ID="rpt_template" runat="server" DataSourceID="src_template" OnItemCommand="rpt_template_ItemCommand">
    <ItemTemplate>
        <p>
            <asp:SqlDataSource ID="src_types" runat="server" ConnectionString="<%$ ConnectionStrings:VConStr %>" SelectCommand='SELECT [type] FROM [templatetype] WHERE [template] = <%# Eval("template") %>' />
            <label for="DropDownList1"><%# Eval("template") %></label>
            <asp:DropDownList ID="DropDownList1" runat="server"  DatasourceID="src_types" DataTextField="type" DataValueField="type" />
        </p>
    </ItemTemplate>
</asp:Repeater>

How do I fill the where-part correctly?


Solution

  • Try something like this:

    <ItemTemplate>
        <asp:HiddenField ID="template" runat="server" 
            Value='<%# Eval("template") %>' 
        />
    
        <asp:SqlDataSource ID="src_types" runat="server" 
            ConnectionString="<%$ ConnectionStrings:VConStr %>" 
            SelectCommand="SELECT [type] FROM [templatetype] WHERE [template] = @template"
        >
            <SelectParameters>
                <asp:ControlParameter
                    Name="template"
                    Type="String"
                    ControlID="template"
                    PropertyName="Value"
                />
            </SelectParameters>
        </asp:SqlDataSource>
    
        ...
    </ItemTemplate>