过滤从sqldatasource填充的Dropdownlist

时间:2012-11-09 06:39:25

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

我有一个gridview,它有两个列类型,从sqldatasources

填充

TransportationMode(已禁用下拉列表),ContainerType(已启用下拉列表),

Vessel  - 20DC
Vessel  - 20RF
Vessel  - 40DC
Vessel  - 40HC
Vessel  - 40RF
Air - Air pick up
Air   - Courier Courier
Truck   - Full Reefer Truck
Truck   - Full Truck
Vessel   - Partial Container
Vessel   - Partial Reefer Container
Truck   - Partial Reefer Truck
Truck   - Partial Truck
Railway - Wagon pick up

问题是我的gridview中连续的TransportationMode被选为“Air”,然后如何过滤ContainerType,并且只应在drowdownlist中填充Air Pick Up和Courier Courier。

1 个答案:

答案 0 :(得分:1)

基本上你需要做的是拥有3个数据源

  1. 一个用于网格视图
  2. 主要下拉列表中的一个
  3. 最后一个儿童下拉列表
  4. 如果您为下拉列表创建了EditItemTemplate字段,则设置为:

        <asp:SqlDataSource
            ID="transportAndContainerTypeDS"
            runat="server"
            ConnectionString="<%$ ConnectionStrings:transportConnection %>"
            SelectCommand="SELECT T.TransportationId, T.TransportationMode, CT.ContainerTypeId, CT.ContainerType FROM Transportation AS T INNER JOIN ContainerType AS CT ON T.TransportationId = CT.TransportationId"></asp:SqlDataSource>
        <asp:GridView ID="gvTransportation"
            runat="server"
            AutoGenerateEditButton="true"
            AutoGenerateColumns="False"
            DataSourceID="transportAndContainerTypeDS"
            DataKeyNames="TransportationId,ContainerTypeId">
            <Columns>
                <asp:BoundField DataField="TransportationId" Visible="false" />
                <asp:BoundField DataField="ContainerTypeId" Visible="false" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Eval("TransportationMode") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlTransportation" runat="server"
                            DataSourceID="transportationDS"
                            AutoPostBack="true"
                            DataValueField="TransportationId"
                            DataTextField="TransportationMode"
                            SelectedValue='<%# Eval("TransportationId") %>'>
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="transportationDS" runat="server"
                            ConnectionString="<%$ ConnectionStrings:transportConnection %>"
                            SelectCommand="SELECT TransportationId,TransportationMode FROM Transportation"></asp:SqlDataSource>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Text='<%# Eval("ContainerType") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlContainerType"
                            runat="server"
                            DataSourceID="containerDS"
                            DataValueField="ContainerTypeId"
                            DataTextField="ContainerType"
                            OnDataBound="ddlContainerType_DataBound" />
                        <asp:SqlDataSource ID="containerDS"
                            runat="server"
                            ConnectionString="<%$ ConnectionStrings:transportConnection %>"
                            SelectCommand="SELECT ContainerTypeId,TransportationId,ContainerType FROM ContainerType WHERE TransportationId = @TransportationId"
                            SelectCommandType="Text">
                            <SelectParameters>
                                <asp:ControlParameter
                                    ControlID="ddlTransportation"
                                    Name="TransportationId"
                                    PropertyName="SelectedValue"
                                    Type="Int32" />
                            </SelectParameters>
                        </asp:SqlDataSource>
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    

    代码背后:

    protected void ddlContainerType_DataBound(object sender, EventArgs e)
    {
        var ddlContainerType = (DropDownList)sender;
        ddlContainerType.Items.Insert(0, new ListItem("Make a Selection", String.Empty));
        ddlContainerType.SelectedIndex = 0;
    
        GridViewRow gvr = (GridViewRow)ddlContainerType.NamingContainer;
        if (gvr.DataItem != null)
        {
            string strModel = ((System.Data.DataRowView)gvr.DataItem)["ContainerType"].ToString();
            ddlContainerType.ClearSelection();
    
            var li = ddlContainerType.Items.FindByValue(strModel);
            if (li != null)
                li.Selected = true;
        }
    }
    

    希望这有帮助!