日期:2014-05-18  浏览次数:20484 次

asp.net 查询后的结果进行翻页
做制网页的时候,经常做,将查询后多条记录进行分页,例如一些搜索引擎网站.我想实现这种功能的方法有很多.
在刚做网页的时候,我的思路是这样的:将查询后的结果做为数据集绑定在一个DataGrid里,然后启用DataGrid里的自动分页.
可是实际上是行不通的,因为在点击"下一页"的时候,整个页面都刷新了,只执行Page_Load()事件,从而致使DataGrid里的数据为空,也就显示不出第二页的数据了.
最后,我换了一种方法,将DataGrid放在一个UpdatePanel,这样以来,在点击"下一页"的时候,就不会出现整个页面刷新了.虽然最终是实现了理想的效果,但是还是想知道有更好的办法!
查询的结果进行分页,我相信大家都做过的,在这里我想请教一下各位网页,你在做这样的功能的时候,思路是怎么样的呢?
谢谢....
若有拍案叫绝的方法,我绝对加分.


------解决方案--------------------
分页...搜索一下到处都能找得到代码
------解决方案--------------------
分页代码很多的,一搜一大堆
------解决方案--------------------
方法太多了啊
我一般用存储过程
------解决方案--------------------
用微软提供的aspnetpager那个控件比较简单,试试
------解决方案--------------------
aspnetpager很好用.特别方便.你找下试试.
------解决方案--------------------
第一种方式行不通的原因是不是你的程序有问题?
典型的写法应该是

//把搜索结果绑定到datagrid
BindDataGrid()
{
dataGrid.DataSource = GetSearchResult();
dataGrid.DataBind();
}

//点击搜索按钮时
SearchButton_Click()
{
BindDataGrid();
}

//点击翻页时
DataGrid_PageIndexChange()
{
 DataGrid.CurrentPageIndex = e.NewPageIndex;
 BindDataGrid(); 
}

------解决方案--------------------
aspnetpager?
------解决方案--------------------
你有没有对datagrid设置允许分页?
------解决方案--------------------
第一种应该行得通的,我这样做都没问题。
------解决方案--------------------
http://www.webdiyer.com/ 最好的分页控件有使用说明和视频教程以及源代码 

例如: 
页面代码======================== 

<webdiyer:AspNetPager ID="AspNetPager1" runat="server" Width="98%" 
AlwaysShow="True" CustomInfoHTML="" CustomInfoTextAlign="Left" 
FirstPageText="首页" HorizontalAlign="Center" LastPageText="尾页" 
NextPageText="下一页" NumericButtonCount="6" PageIndexBoxType="TextBox" 
PrevPageText="前一页" ShowCustomInfoSection="Left" ShowPageIndexBox="Always" 
UrlPaging="True" PageSize="10" onpagechanging="AspNetPager1_PageChanging"> 
</webdiyer:AspNetPager> 

后台AspNetPager1_PageChanging事件代码 

if (!this.IsPostBack) 

this.AspNetPager1.CurrentPageIndex = e.NewPageIndex; 
this.AspNetPager1.RecordCount = BLL.Userinfo.GetNum(); 
this.AspNetPager1.CustomInfoHTML = "总共" + this.AspNetPager1.RecordCount.ToString() + "条数据,共" + this.AspNetPager1.PageCount.ToString() + "页/目前第" + this.AspNetPager1.CurrentPageIndex.ToString() + "页";}
------解决方案--------------------
DataGrid可以使用它默认的分页功能
也可以 自定义分页
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class APage : System.Web.UI.UserControl
{
PagedDataSource objPage;
public int CurPage;//定义变量来保存当前页索引
public static int TotalPage;//定义变量来保存总页数