Simple implementation of ASP. NET refreshing pagination and asp.net refreshing pagination
The following are all the steps for implementing the refreshing paging function.
1. Create a paging stored procedure:
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. Set the Ajax Control
Ajax will certainly be used for implementation without refreshing the content. The first step is to add the necessary:
<Asp: ScriptManager ID = "ScriptManager1" runat = "server">
</Asp: ScriptManager>
Then embed the Repeater control into 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> <em> posting time: <% # Eval ("addtime ") %> </em> </p> </li> </ItemTemplate> </asp: Repeater> </ul> <p> total record: <asp: literal ID = "ltlCount" runat = "server"> </asp: Literal> </p> <webdiyer: aspNetPager ID = "AspNetPager1" runat = "server" AlwaysShow = "true" PageIndexBoxType = "DropDownList"> </webdiyer: aspNetPager> </p> </div> </ContentTemplate> </asp: UpdatePanel>
3. AspNetPager Page Control
We can see that the above Code has a paging control. I believe that this paging control is no stranger to everyone, so I will not introduce it much. AspNetPager. dll must be available and referenced in the project. Introduce this code in the header:
<% @ Register Assembly = "AspNetPager" Namespace = "Wuqi. Webdiyer" TagPrefix = "webdiyer" %>
At this time, the paging control should be ready for use. Finally, bind the data in the background and the AspNetPager control. The complete background code is as follows:
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; // the current Page number int pageSize = 5; // number of records displayed on each Page protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) {currentPageNumber = 1; ViewState ["currentPageNumber"] = currentPageNumber; BindData ();} AspNetPager1.PageChanged + = new EventHandler (AspNetPager1_PageChanged ); // define control paging events} // obtain the total number of records 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 ;}// bind the 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); // call the stored procedure if (dt. rows. count> 0) {AspNetPager1.PageSize = PageSize; AspNetPager1.RecordCount = GetCount (); AspNetPager1.CurrentPageIndex = currentPageNumber; this. repeater1.DataSource = dt. defaultView; this. callback () ;}// pagination event void aspnetpager;pagechanged (object sender, EventArgs e) {currentPageNumber = AspNetPager1.CurrentPageIndex; ViewState ["currentPageNumber"] = currentPageNumber; BindData ();} // read the stored procedure and return the table private DataTable GetTa Ble (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 ();}}}}
At this point, the basic success has been achieved. Although it is relatively simple, it is worth collecting and learning the knowledge points related to stored procedures, Ajax, and paging controls, providing better methods is the motivation for my learning.