下面就是和大家分享的實現無刷新分頁功能的全部步驟,歡迎學習。
1、新建一個分頁存儲過程:
CREATE procedure [dbo].[P_Pager] (@PageNumber int, @PageSize int) as declare @sql nvarchar(4000) set @sql = 'select top ' + Convert(varchar, @PageSize) + ' * from T_Test where [type]=1 and id not in (select top ' + Convert(varchar, (@PageNumber - 1) * @PageSize) + ' id from T_Test order by id desc) order by id desc' exec sp_executesql @sql GO
2、設置Ajax控件
想要實現無刷新,肯定會用到Ajax,首先是要放入必不可少的:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
接著把Repeater控件嵌入到UpdatePanel裡面去:
<asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <ul> <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <li> <p> <span><%#Eval("username") %>:</span><%#Eval("content").ToString())) %></p> <p> <em>發表時間: <%#Eval("addtime") %> </em> </p> </li> </ItemTemplate> </asp:Repeater> </ul> <p> 總記錄:<asp:Literal ID="ltlCount" runat="server"></asp:Literal></p> <p> <webdiyer:AspNetPager ID="AspNetPager1" runat="server" AlwaysShow="true" PageIndexBoxType="DropDownList" > </webdiyer:AspNetPager> </p> </div> </ContentTemplate> </asp:UpdatePanel>
3、AspNetPager分頁控件
可以看到以上的代碼有個分頁控件,我相信這個分頁控件大家都不陌生了,我就不多介紹。必須有AspNetPager.dll,然後在項目中引用,在頭部引入這一段代碼:
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
這時候分頁控件應該就可以使用了。最後在後台綁定一下數據,同時也要綁定AspNetPager控件,完整的後台代碼如下:
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using Wuqi.Webdiyer; using System.Data.SqlClient; public partial class AjaxPager : System.Web.UI.Page { int currentPageNumber;//當前頁號 int pageSize = 5;//每頁顯示記錄條數 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { currentPageNumber = 1; ViewState["currentPageNumber"] = currentPageNumber; BindData(); } AspNetPager1.PageChanged += new EventHandler(AspNetPager1_PageChanged); //定義控件分頁事件 } //獲取記錄總數 private int GetCount() { string sql = "select COUNT(*) from T_Test"; DataTable dt = GetTable(sql, CommandType.Text, values); if (dt.Rows.Count > 0) { return Convert.ToInt32(dt.Rows[0][0]); } else { return 0; } } //綁定數據 private void BindData() { ltlCount.Text = GetCount().ToString(); currentPageNumber = Convert.ToInt32(ViewState["currentPageNumber"]); SqlParameter[] values = { new SqlParameter("@PageNumber", currentPageNumber), new SqlParameter("@PageSize", pageSize) }; DataTable dt = GetTable("P_Pager", CommandType.StoredProcedure, values); //調用存儲過程 if (dt.Rows.Count > 0) { AspNetPager1.PageSize = pageSize; AspNetPager1.RecordCount = GetCount(); AspNetPager1.CurrentPageIndex = currentPageNumber; this.Repeater1.DataSource = dt.DefaultView; this.Repeater1.DataBind(); } } //分頁事件 void AspNetPager1_PageChanged(object sender, EventArgs e) { currentPageNumber = AspNetPager1.CurrentPageIndex; ViewState["currentPageNumber"] = currentPageNumber; BindData(); } // 讀取存儲過程返回table private DataTable GetTable(string sql, CommandType t, params SqlParameter[] values) { using (SqlConnection conn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=testDB;Persist Security Info=True;User ID=sa;Password=123456")) { SqlCommand comm = new SqlCommand(sql, conn); comm.CommandType = t; if (values != null && values.Length > 0) comm.Parameters.AddRange(values); SqlDataAdapter da = new SqlDataAdapter(comm); DataSet ds = new DataSet(); try { conn.Open(); da.Fill(ds); return ds.Tables[0]; } catch (Exception) { return null; } finally { conn.Close(); conn.Dispose(); } } } }
至此,基本大功告成,雖然比較簡單,但是涉及到存儲過程,Ajax,分頁控件等知識點,還是蠻值得收藏和學習的,希望我的這篇文章是拋磚引玉,讓大家提供更好的方法,才是我學習的動力。