ASP.NET之AJAX分页步骤

一、在ASPX页面中的代码部分
 
第一步:在aspx页面中注册组件
-----------------------------------------
<%@ Register Assembly= "AspNetPager"  Namespace= "Wuqi.Webdiyer"  TagPrefix= "webdiyer"  %>
 
第二步:在<form id= "form_base"  runat= "server" ></form>中加入ScriptManager
-----------------------------------------
 
<asp:ScriptManager runat= "server"  ID= "scriptManager" >
</asp:ScriptManager>
 
第三步:用UpdatePanel和ContentTemplate包起来  UpdatePanel 中的UpdateMode= "Conditional" 很重要
 
<asp:UpdatePanel ID= "UpdatePanel1"  UpdateMode= "Conditional"  runat= "server" >
<ContentTemplate>
 
<!--循环数据开始-->
<asp:Repeater ID= "Repeater1"   runat= "server" >
<HeaderTemplate>
<table>
      <tr>
         <th>订单号</th>
         <th>预约商家</th>
         <th>预约时间</th>
      </tr>
</HeaderTemplate>
<ItemTemplate>
      <tr>
          <td><%#Eval( "orderID" )%></td>
          <td><%#Eval( "cname" )%></td>
          <td><%#Eval( "jtime" )%></td>
      </tr>              
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<!--循环数据结束-->
 
<!--分页开始-->
<div id= "page" >
<webdiyer:aspnetpager id= "AspNetPager1"  runat= "server"  onpagechanging= "AspNetPager1_PageChanging"  FirstPageText= "首页"  LastPageText= "尾页"  NextPageText= "下一页"  PageSize= "3"  PrevPageText= "上一页"  AlwaysShow= "True" ></webdiyer:aspnetpager>
<asp:Label ID= "Label1"  runat= "server"  Visible= "False" ></asp:Label>
</div>
<!--分页结束-->
</ContentTemplate>
</asp:UpdatePanel>
 
二、在ASPX.CS代码中的部分
 
第一步:在Page_Load()中加入
protected  void  Page_Load( object  sender, EventArgs e)
{
         if  (!IsPostBack)
         {
         //为AspNetPager1控件注册一个异步回发方式
         scriptManager.RegisterAsyncPostBackControl(AspNetPager1);
 
         string  sqlStr = "select * from jorder where pname='"  + this .jname.Text.ToString() + "' order by pstatus,orderID desc" ;
                 Label1.Text = sqlStr.ToString(); //分页,绑定SQL语句
                 Pagebangding(Label1.Text); //分页绑定
     }
}
 
第二步:加入方法两个
/// <summary>
/// 分页绑定数据
/// </summary>
private  void  Pagebangding( string  sql)
{
         this .AspNetPager1.RecordCount = new  DataBaseSql().GetDataTableCount(sql);
         if  ( this .AspNetPager1.RecordCount > 0)
         {
             countStr = this .AspNetPager1.RecordCount.ToString(); //记录数
         }
         else
         {
             countStr = "0" ;
         }
         DataSet ds = new  DataBaseSql().GetPageDataSet( this .AspNetPager1.PageSize, this .AspNetPager1.CurrentPageIndex, sql, "jorder" ); //"jorder"为数据库表名称
         this .Repeater1.DataSource = ds;
         this .Repeater1.DataBind();
}
 
/// <summary>
/// 分页
/// </summary>
protected  void  AspNetPager1_PageChanging( object  src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
         //调用UpdatePanel的Update()方法更新内容
         UpdatePanel1.Update();
         //设置更新的模式(比较重要,如果为Always就无法实现AJAX分页)
         UpdatePanel1.UpdateMode = UpdatePanelUpdateMode.Conditional;
 
         this .AspNetPager1.CurrentPageIndex = e.NewPageIndex;
         this .ViewState[ "index" ] = e.NewPageIndex.ToString();
         Pagebangding(Label1.Text); //绑定数据
}

你可能感兴趣的:(分页,asp.net)