GridView中过滤的DropDownList

时间:2013-04-24 11:27:14

标签: asp.net .net gridview

DropDownList嵌套在GridView中。如何过滤DropDownList数据源,仅显示活动数据库行(列为活动状态作为位类型)或当前选定的DropDownList值?

以下代码的一部分不起作用。有没有办法如何绑定到GridView行数据(通过标记)?

<asp:GridView ID="GridView" runat="server" AllowPaging="True" 
    AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="PlasticTypeId" 
    DataSourceID="dsPlasticTypes">
    <Columns>
        <asp:CommandField ShowEditButton="True" />
        <asp:CheckBoxField DataField="Active" HeaderText="Active" SortExpression="Active" />
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
        <asp:TemplateField HeaderText="<%$ Resources:Labels, PlasticFamily %>" SortExpression="PlasticFamily.Name">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("PlasticFamily.Name") %>'/>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:DropDownList ID="ddlFamilies" runat="server" DataSourceID="dsFamilies" DataValueField="PlasticFamilyId"
                    DataTextField="Name" SelectedValue='<%# Bind("PlasticFamilyId") %>'/>
                <asp:EntityDataSource ID="dsFamilies" runat="server" 
                    ConnectionString="name=PlasticsDbEntities" OrderBy="it.Name"
                    DefaultContainerName="PlasticsDbEntities" EnableFlattening="False" 
                    EntitySetName="PlasticFamilies"
                    Where="it.Active==true || it.PlasticFamilyId==@control">
                    <WhereParameters>
                        <asp:ControlParameter Name="control" ControlID="ddlFamilies" PropertyName="SelectedValue" Type="Int32" />
                    </WhereParameters>
                </asp:EntityDataSource>
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

1 个答案:

答案 0 :(得分:1)

我通过将DropDownList绑定上方的隐藏字段放到同一字段并将其用作控制参数来过滤数据源来解决这个问题。

<asp:TemplateField HeaderText="<%$ Resources:Labels, PlasticFamily %>" SortExpression="PlasticFamily.Name">
    <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Eval("PlasticFamily.Name") %>' />
    </ItemTemplate>
    <EditItemTemplate>
        <asp:HiddenField ID="hfPlasticFamilyId" runat="server" Value='<%# Eval("PlasticFamilyId") %>' />
        <asp:DropDownList ID="ddlFamilies" runat="server" DataSourceID="dsFamilies" 
            DataTextField="Name" DataValueField="PlasticFamilyId" SelectedValue='<%# Bind("PlasticFamilyId") %>' />
        <asp:EntityDataSource ID="dsFamilies" runat="server" 
            ConnectionString="name=PlasticsDbEntities" 
            DefaultContainerName="PlasticsDbEntities" EnableFlattening="False" 
            EntitySetName="PlasticFamilies" OrderBy="it.Name" 
            Where="it.Active==true || it.PlasticFamilyId==@control">
            <WhereParameters>
                <asp:ControlParameter ControlID="hfPlasticFamilyId" Name="control" 
                    PropertyName="Value" Type="Int32" />
            </WhereParameters>
        </asp:EntityDataSource>
    </EditItemTemplate>
</asp:TemplateField>