[英]Custom paging in asp.net without data source controls using Entity Framework
I am trying to implement custom pagination in gridview without using any data source controls. 我正在尝试在gridview中实现自定义分页而不使用任何数据源控件。 I didn't find any good/complete tutorial so far.
到目前为止,我没有找到任何好的/完整的教程。
Here is how I am creating the gridview: 这是我创建gridview的方法:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="10"
onpageindexchanging="GridView1_PageIndexChanging" ShowFooter="True">
</asp:GridView>
This grid is populated when a button is clicked. 当单击按钮时,将填充此网格。 Rows from the database need to be picked based on items in listbox control
需要根据列表框控件中的项目选择数据库中的行
protected void btnSearch_Click(object sender, EventArgs e)
{
using (context = new TrackForceDataEntities())
{
try
{
string[] arr = lstStates.Items.Cast<ListItem>().Select(i => i.Text).ToArray();
var searchResults = context.data_vault.Where(d => arr.Contains(d.STATE)).OrderByDescending(d=>d.STATE);
GridView1.DataSource = searchResults;
GridView1.DataBind();
}
catch (Exception exception)
{
Response.Write(exception.Message);
}
}
}
This above query is very slow because it is bringing all records, How can I fetch only one page records and still show paging ? 上面的查询非常慢,因为它带来了所有记录。我如何仅获取一页记录并仍然显示分页?
Next is the gridview PageIndexChanging
event. 接下来是gridview
PageIndexChanging
事件。 When I click page 2, it shows page 2 rows but paging links disappear. 当我单击第2页时,它显示第2页的行,但分页链接消失了。
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
using (context = new TrackForceDataEntities())
{
try
{
// var searchResults = context.data_vault.Where(d => d.STATE == lstStates.SelectedItem.Text).OrderBy(d => d.dv_id).Take(10).Skip(2);
//var selection = lstStates.Items(i => i.Text).ToArray();
//var result = context.data_vault.Where(x => selection.Contains(x.Prop));
string[] arr = lstStates.Items.Cast<ListItem>().Select(i => i.Text).ToArray();
var result = context.data_vault.Where(d => arr.Contains(d.STATE)).OrderByDescending(d => d.STATE).Skip(e.NewPageIndex * GridView1.PageSize)
.Take(GridView1.PageSize)
.ToList();
//var result = context.data_vault.Where(d => d.STATE == lstStates.SelectedItem.Text).OrderBy(d => d.STATE).Skip(e.NewPageIndex * GridView1.PageSize).Take(GridView1.PageSize).ToList();
//// this is very important part too
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = result;
GridView1.DataBind();
}
catch (Exception exception)
{
Response.Write(exception.Message);
}
}
}
Go Through The following link You will able to get the answer. 通过以下链接您将可以获得答案。 The paging had done through the sql server by passing the page number and page size .
分页是通过sql服务器传递页码和页大小来完成的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.