Filtering Dropdownlist populated from sqldatasource

纵饮孤独 提交于 2019-12-12 05:27:48

问题


I have a gridview which has 2 column types which are populated from sqldatasources

TransportationMode(Disabled Dropdownlist), ContainerType(Enabled Dropdownlist),

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

Question is when TransportationMode of a row in my gridview has been choosen as 'Air' for example, Then how can ContainerType be filtered and only Air Pick Up and Courier Courier should be populated in the drowdownlist.


回答1:


Basically what you need to do is have 3 data sources

  1. One for the grid view
  2. One for the main drop down list
  3. And a last one for the child drop down list

Once you have that create EditItemTemplate fields for the drop downs and you're set:

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

Code behind:

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

Hope this helps!



来源:https://stackoverflow.com/questions/13303297/filtering-dropdownlist-populated-from-sqldatasource

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