Simple implementation of ASP. NET non refresh paging
- 2021-07-06 10:42:44
- OfStack
The following are all the steps to realize the non-refresh paging function shared with you. Welcome to learn.
1. Create a new 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
If you want to achieve no refresh, you will definitely use Ajax. First, you must put in the essential:
<
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>
<p>
<em> Published at:
<%#Eval("addtime") %>
</em>
</p>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
<p>
Total records: <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 paging control
You can see that the above code has a paging control, I believe this paging control everyone is no stranger, I will not introduce more. You must have AspNetPager. dll, then reference it in the project, introducing this 1 piece of code in the header:
<
%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %
>
At this time, the paging control should be available. Finally, bind the data under 1 in the background, and bind the AspNetPager control at the same time. 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;// Current page number
int pageSize = 5;// Number of records displayed per page
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
currentPageNumber = 1;
ViewState["currentPageNumber"] = currentPageNumber;
BindData();
}
AspNetPager1.PageChanged += new EventHandler(AspNetPager1_PageChanged); // Defining Control Paging Events
}
// Get 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 data
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); // Calling stored procedures
if (dt.Rows.Count > 0)
{
AspNetPager1.PageSize = pageSize;
AspNetPager1.RecordCount = GetCount();
AspNetPager1.CurrentPageIndex = currentPageNumber;
this.Repeater1.DataSource = dt.DefaultView;
this.Repeater1.DataBind();
}
}
// Paging event
void AspNetPager1_PageChanged(object sender, EventArgs e)
{
currentPageNumber = AspNetPager1.CurrentPageIndex;
ViewState["currentPageNumber"] = currentPageNumber;
BindData();
}
// Read stored procedure returns 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();
}
}
}
}
At this point, it is basically done. Although it is relatively simple, it is worth collecting and learning knowledge points such as stored procedures, Ajax, paging controls, etc. I hope my article is to attract more attention and let everyone provide better methods, which is the motivation for me to learn.