ASP.NET中大量数据的分页、排序与过滤

1、背景

在ASP.NET中通常是用GridView显示数据,然而如果要显示大量数据,一次性的取得所有的数据d的方法指定是不行的。

2、解决方法

在分页的时候请求当页的数据,排序的时候请求排序后的当页数据,过滤的时候显示过滤后的数据。

因此服务端要提供排序、分页以及过滤的参数,在每一次请求的时候传递相应条件返回相应的数据。

        public List<DeviceState> GetDeviceStateList(string sortExpression, int index, int size, bool isPrinterOutOfPaper,
            bool isNetworkError, bool isHardwareError, string startDeviceID, string endDeviceID, string deviceType,
            string version, string startRefreshInterval, string endRefreshInterval, DateTime startPowerOn, DateTime endPowerOn, DateTime startPowerOff,
            DateTime endPowerOff, string startVolume, string endVolume)
        {
            return ChannelManager.Instance.DeviceService.GetDeviceStateList(
                new GetDeviceStateListRequest()
            {
                SortExpression = sortExpression,
                Index = index,
                Count = size,
                Filter = new FilterDescription() 
                { 
                    IsPrinterOutOfPaper = isPrinterOutOfPaper, 
                    IsHardwareError = isHardwareError, 
                    IsNetworkError = isNetworkError, 
                    StartDeviceID = startDeviceID,
                    EndDeviceID = endDeviceID,
                    DeviceType = deviceType,
                    Version = version, 
                    StartRefreshInterval = Convert.ToInt32(startRefreshInterval),
                    EndRefreshInterval = Convert.ToInt32(endRefreshInterval),
                    StartPowerOn = startPowerOn,
                    EndPowerOn = endPowerOn,
                    StartPowerOff = startPowerOff,
                    EndPowerOff = endPowerOff,
                    StartVolume = Convert.ToDouble(startVolume), 
                    EndVolume = Convert.ToDouble(endVolume)
                }
            }); 
        }

为了方便使用,我们可以使用ObjectDataSource配合GridView调用该函数,ObjectDataSource内置了对分页、排序的参数传递,但是对于排序需要写格外的SelectParameter传递数据,例如:

                <asp:ObjectDataSource ID="DeviceStateDataSource" runat="server" EnablePaging="True"
                    MaximumRowsParameterName="size" SelectCountMethod="GetDeviceCount" SelectMethod="GetDeviceStateList"
                    StartRowIndexParameterName="index" TypeName="ICardPay.Web.DeviceManagerClient"
                    SortParameterName="sortExpression">
                    <SelectParameters>
                        <asp:ControlParameter Name="isPrinterOutOfPaper" ControlID="IsOutOfPaperCheckBox"
                            PropertyName="Checked" />
                        <asp:ControlParameter Name="isNetworkError" ControlID="IsNetworkErrorCheckBox" PropertyName="Checked" />
                        <asp:ControlParameter Name="isHardwareError" ControlID="IsHardwareErrorCheckBox"
                            PropertyName="Checked" />
                        <asp:ControlParameter Name="startDeviceID" ControlID="DeviceIDFromTextBox" PropertyName="Text" />
                        <asp:ControlParameter Name="endDeviceID" ControlID="DeviceIDToTextBox" PropertyName="Text" />
                        <asp:ControlParameter Name="deviceType" ControlID="DeviceTypeTextBox" PropertyName="Text" />
                        <asp:ControlParameter Name="version" ControlID="VersionTextBox" PropertyName="Text" />
                        <asp:ControlParameter Name="startRefreshInterval" ControlID="RefreshIntervalFromTextBox"
                            PropertyName="Text" />
                        <asp:ControlParameter Name="endRefreshInterval" ControlID="RefreshIntervalToTextBox"
                            PropertyName="Text" />
                        <asp:ControlParameter Name="startPowerOn" ControlID="TimeSelector1" PropertyName="Date" />
                        <asp:ControlParameter Name="endPowerOn" ControlID="TimeSelector2" PropertyName="Date" />
                        <asp:ControlParameter Name="startPowerOff" ControlID="TimeSelector3" PropertyName="Date" />
                        <asp:ControlParameter Name="endPowerOff" ControlID="TimeSelector4" PropertyName="Date" />
                        <asp:ControlParameter Name="startVolume" ControlID="VolumeTextBox1" PropertyName="Text" />
                        <asp:ControlParameter Name="endVolume" ControlID="VolumeTextBox2" PropertyName="Text" />
                    </SelectParameters>
                </asp:ObjectDataSource>

最后GridView的写法如下:

                <asp:UpdatePanel runat="server">
                    <ContentTemplate>
                        <asp:GridView ID="DeviceStateGridView" runat="server" AllowPaging="True" PageSize="15"
                            AllowSorting="true" Width="100%" OnRowCreated="DeviceStateGridViewRowCreated"
                            AutoGenerateColumns="False" DataSourceID="DeviceStateDataSource" OnDataBound="DeviceStateGridDataBound">
                            <Columns>
                                <asp:TemplateField>
                                    <HeaderTemplate>
                                        <asp:CheckBox ID="SelectAllCheckBox" runat="server" AutoPostBack="true" OnCheckedChanged="SelectAllCheckBoxCheckedChanged" />
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <asp:CheckBox ID="SelectCheckBox" runat="server" AutoPostBack="true" OnCheckedChanged="SelectCheckBoxCheckedChanged" />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:BoundField DataField="DeviceID" HeaderText="编号" SortExpression="DeviceID" />                             </Columns>
                            <EmptyDataTemplate>
                                <p>
                                    满足条件的数据不存在!</p>
                            </EmptyDataTemplate>
                        </asp:GridView>
                    </ContentTemplate>
                </asp:UpdatePanel>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值