1、背景
在ASP.NET中通常是用GridView显示数据,然而如果要显示大量数据,一次性的取得所有的数据d的方法指定是不行的。
2、解决方法
在分页的时候请求当页的数据,排序的时候请求排序后的当页数据,过滤的时候显示过滤后的数据。
因此服务端要提供排序、分页以及过滤的参数,在每一次请求的时候传递相应条件返回相应的数据。
class="dp-c">
- 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>