Постраничный вывод информации в GridView с использованием LinqDataSource
Технология Asp.Net WebForms
Решил использовать для этого GridView а для заполнения данными LinqDataSource.
Создаю сам грид
Код:
<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>
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>
Код:
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()
};
}
{
e.Result = from p in _employees
select new
{
EmployeeId = p.EmployeeId,
EmployeeName = p.EmployeeName,
EmployeeSalary = p.CalculateMonthlySalary()
};
}
Если сделать заполнение так:
Код:
<asp:GridView ID="gvEmployees"
runat="server"
AllowPaging="True"
PageSize="<%# PAGE_SIZE %>"
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>
runat="server"
AllowPaging="True"
PageSize="<%# PAGE_SIZE %>"
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>
Код:
protected const int PAGE_SIZE = 5;
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.Arguments.TotalRowCount = query.Count();
query = query.Skip(gvEmployees.PageIndex * PAGE_SIZE).Take(PAGE_SIZE);
e.Result = query;
}
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.Arguments.TotalRowCount = query.Count();
query = query.Skip(gvEmployees.PageIndex * PAGE_SIZE).Take(PAGE_SIZE);
e.Result = query;
}
Как правильно сделать постраничный вывод данных?
Правильна ли такая реализация?
Спасибо!