为什么DropDownList过滤器不能用于我的GridView

时间:2014-08-08 12:27:26

标签: c# asp.net gridview filter

我有以下代码,DropDownList中的每列都有GridView

<asp:UpdatePanel runat="server" ClientIDMode="Static" ID="TasksUpdatePanel" UpdateMode="Conditional">
    <ContentTemplate>
        <table style="width: 100%;" border="0" cellpadding="0" cellspacing="1" class="GridviewTable">
            <tr>
                <td style="width: 25%;">
                    <asp:DropDownList ID="ddlTaskName" DataSourceID="dsPopulateTaskName" AutoPostBack="false" DataValueField="Task Name" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true">
                        <asp:ListItem Text="All" Value="%"></asp:ListItem>
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="dsPopulateTaskName" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand="SELECT DISTINCT [ATTR2739] 'Task Name' FROM HSI.RMOBJECTINSTANCE1224 CT LEFT JOIN HSI.USERGROUP UG on CT.FK2743 = UG.USERGROUPNUM WHERE CT.ACTIVESTATUS = 0"></asp:SqlDataSource>
                </td>
                <td style="width: 20%;">
                    <asp:DropDownList ID="ddlService" DataSourceID="dsPopulateService" AutoPostBack="false" DataValueField="Service" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true">
                        <asp:ListItem Text="All" Value="%"></asp:ListItem>
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="dsPopulateService" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand="SELECT DISTINCT [ATTR2846] 'Service' FROM HSI.RMOBJECTINSTANCE1224 CT LEFT JOIN HSI.USERGROUP UG on CT.FK2743 = UG.USERGROUPNUM WHERE CT.ACTIVESTATUS = 0"></asp:SqlDataSource>
                </td>
                <td style="width: 15%;">
                    <asp:DropDownList ID="ddlStatus" DataSourceID="dsPopulateStatus" AutoPostBack="false" DataValueField="Status" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true">
                        <asp:ListItem Text="All" Value="%"></asp:ListItem>
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="dsPopulateStatus" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand="SELECT DISTINCT [ATTR2812] 'Status' FROM HSI.RMOBJECTINSTANCE1224 CT LEFT JOIN HSI.USERGROUP UG on CT.FK2743 = UG.USERGROUPNUM WHERE CT.ACTIVESTATUS = 0"></asp:SqlDataSource>
                </td>
                <td style="width: 15%;">
                    <asp:DropDownList ID="ddlDueDate" DataSourceID="dsPopulateDueDate" AutoPostBack="false" DataValueField="Due Date" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true">
                        <asp:ListItem Text="All" Value="%"></asp:ListItem>
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="dsPopulateDueDate" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand="SELECT DISTINCT CONVERT(VARCHAR(14), [ATTR2752], 110) 'Due Date' FROM HSI.RMOBJECTINSTANCE1224 CT LEFT JOIN HSI.USERGROUP UG on CT.FK2743 = UG.USERGROUPNUM WHERE CT.ACTIVESTATUS = 0"></asp:SqlDataSource>
                </td>
                <td style="width: 15%;">
                    <asp:DropDownList ID="ddlClient" DataSourceID="dsPopulateClient" AutoPostBack="false" DataValueField="Client" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true">
                        <asp:ListItem Text="All" Value="%"></asp:ListItem>
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="dsPopulateClient" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand="SELECT DISTINCT [ATTR2799] 'Client' FROM HSI.RMOBJECTINSTANCE1224 CT LEFT JOIN HSI.USERGROUP UG on CT.FK2743 = UG.USERGROUPNUM WHERE CT.ACTIVESTATUS = 0"></asp:SqlDataSource>
                </td>
            </tr>
        </table>
        <asp:GridView ShowHeaderWhenEmpty="false" AlternatingRowStyle-BackColor="#EBE9E9" AutoGenerateColumns="false" OnSorting="yourTasksGV_Sorting" AllowSorting="true" ID="yourTasksGV" runat="server" ClientIDMode="Static" EmptyDataText="There is no data to display" OnRowDataBound="yourTasksGV_RowDataBound" OnRowCreated="yourTasksGV_RowCreated">
            <Columns>
                <asp:HyperLinkField Target="_blank" DataNavigateUrlFields="Task Detail" DataTextField="Task Name" DataNavigateUrlFormatString="" HeaderText="Task Detail" SortExpression="Task Name" ItemStyle-Width="25%" ItemStyle-CssClass="taskTableColumn" />
                <asp:BoundField DataField="Service" HeaderText="Service" SortExpression="Service" ItemStyle-Width="20%" ItemStyle-CssClass="taskTableColumn" />
                <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" ItemStyle-Width="15%" ItemStyle-CssClass="taskTableColumn" />
                <asp:BoundField DataField="Due Date" HeaderText="Due Date" SortExpression="Due Date" ItemStyle-Width="15%" ItemStyle-CssClass="taskTableColumn" />
                <asp:BoundField DataField="Client" HeaderText="Client" SortExpression="Client" ItemStyle-Width="15%" ItemStyle-CssClass="taskTableColumn" />
            </Columns>
        </asp:GridView>
         <asp:SqlDataSource ID="dsdetails" runat="server" ConnectionString="<%$ConnectionStrings:gvConnString %>" SelectCommand="SELECT 'http://dmag.com/' + CAST(CT.OBJECTID AS VARCHAR) + '&classid=1224' 'Task Detail', CT.ATTR2846 'Service', CT.ATTR2812 'Status', CT.ATTR2752 'Due Date', CT.ATTR2739 'Task Name', CT.ATTR2799 'Client' FROM HSI.RMOBJECTINSTANCE1224 CT LEFT JOIN HSI.USERGROUP UG on CT.FK2743 = UG.USERGROUPNUM WHERE CT.ACTIVESTATUS = 0" FilterExpression=" [Task Name] Like '{4}%' and [Service] Like '{1}%' and [Status] Like '{2}%' and [Due Date] Like '{3}%' and [Client] Like '{5}%'">
        <FilterParameters>
            <asp:ControlParameter Name="Task Name" ControlID="ddlTaskName" PropertyName="SelectedValue" />
            <asp:ControlParameter Name="Service" ControlID="ddlService" PropertyName="SelectedValue" />
            <asp:ControlParameter Name="Status" ControlID="ddlStatus" PropertyName="SelectedValue" />
            <asp:ControlParameter Name="Due Date" ControlID="ddlDueDate" PropertyName="SelectedValue" />
            <asp:ControlParameter Name="Client" ControlID="ddlClient" PropertyName="SelectedValue" />
        </FilterParameters>
        </asp:SqlDataSource>
    </ContentTemplate>
</asp:UpdatePanel>

DropDownList显示每列的正确值,但是当我选择一个选项时,GridView未过滤。

我正在使用UpdatePanel,因此页面在排序时不会进行回发,因此DropDownList AutoPostBack设置为false。

更新:我错过了DataSourceID中的GridView,但这是因为我正在填充代码并将其排序。

如何解决问题?

1 个答案:

答案 0 :(得分:1)

即使使用updatepanel,它也不会过滤客户端的数据。它必须到服务器端过滤数据。你必须做

                AutoPostBack = true

并处理

                SelectedIndex_Change event

然后你打电话给

               updatepanel.update()

要处理Change事件,您必须在网格数据源上应用过滤器并再次绑定网格。