1

我想用 asp.net 和 C# 做以下事情:

SqlDataSource 从数据库中获取模板列表。然后,在转发器中,为每个模板创建一个 DropDownList 元素,其中包含为每个模板找到的类型。这可能吗?

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

如何正确填写 where-part?

4

1 回答 1

1

尝试这样的事情:

<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>
于 2012-12-19T14:11:56.397 回答