Good day.
It is necessary to display 5 entries on the page with the ability to move to another page.

Asp.Net WebForms Technology

I decided to use a GridView for this and to fill in the LinqDataSource data.

I create the grid myself

 <asp:GridView ID="gvEmployees" runat="server" AllowPaging="True" PageSize="5" AllowSorting="True" DataSourceID="LinqDataSource1" EmptyDataText="Элементов нет" AutoGenerateColumns="False"> <Columns> <asp:BoundField SortExpression="Title" DataField="EmployeeId" HeaderText="ID" /> <asp:BoundField SortExpression="Title" DataField="EmployeeName" HeaderText="Name" /> <asp:BoundField SortExpression="Title" DataField="EmployeeSalary" HeaderText="Salary" /> </Columns> </asp:GridView> <asp:LinqDataSource ID="LinqDataSource1" runat="server" OnSelecting="LinqDataSource1_Selecting"></asp:LinqDataSource> 

Fill it up like this:

  protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e) { e.Result = from p in _employees select new { EmployeeId = p.EmployeeId, EmployeeName = p.EmployeeName, EmployeeSalary = p.CalculateMonthlySalary() }; } 

The problem is that when you move through the pages, the data is loaded by the new one and the data is added to GridViev .

How to make paginated output of data?

Is this implementation correct?

Thank!

    1 answer 1

    Working version (I can throw off the project):

     <asp:GridView ID="gvEmployees" runat="server" AllowPaging="True" AllowSorting="True" DataSourceID="LinqDataSource1" PageSize="5" AutoGenerateColumns="False"> <Columns> <asp:BoundField SortExpression="Title" DataField="EmployeeId" HeaderText="ID" /> <asp:BoundField SortExpression="Title" DataField="EmployeeName" HeaderText="Name" /> <asp:BoundField SortExpression="Title" DataField="EmployeeSalary" HeaderText="Salary" /> </Columns> </asp:GridView> <asp:LinqDataSource ID="LinqDataSource1" runat="server" AutoGenerateOrderByClause="True" AutoGenerateWhereClause="True" OnSelecting="LinqDataSource1_Selecting"></asp:LinqDataSource> protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e) { var query = from p in _employees select new { EmployeeId = p.EmployeeId, EmployeeName = p.EmployeeName, EmployeeSalary = p.CalculateMonthlySalary() }; e.Result = query; } 
    • Does not exceed. With such an implementation, the page navigation buttons disappear ... :( - Telsystems
    • Did you both use the solutions at the same time? Correct only the code - Pavel Azanov
    • I tried both options in turn. If you use the string e.Result = query.Skip (gvEmployees.PageIndex * PAGE_SIZE) .Take (PAGE_SIZE); or query = query.Skip (gvEmployees.PageIndex * PAGE_SIZE) .Take (PAGE_SIZE); e.Result = query; Then the navigation buttons disappear. if you do not use this line, then a full data load will occur. :( - Telsystems
    • try the second option, with AutoPage = "false" set on GridView - Pavel Azanov
    • <asp: GridView ID = "gvEmployees" runat = "server" AutoPage = "false" AllowPaging = "True" PageSize = "<% # PAGE_SIZE%>" AllowSorting = "True" DataSourceID = "LinqDataSource1" EmptyDataText = "No items" AutoGenerateColumns = "false"> same problem. no navigation buttons are displayed. if you put AutoPage = "true" - too. if you change the values ​​of AllowPaging - the same thing: ( - Telsystems