Asp. NET no refresh paging simple implementation _ practical skills

Source: Internet
Author: User

Here is a share of the implementation of the No refresh paging function of all the steps, 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 (varcha R, @PageSize)  + ' * from t_test where [type]=1 and ID not in (select top ' + Convert (varchar, (@PageNumber-1) * @Pag Esize) + ' ID from t_test ORDER BY id DESC '-ORDER BY id DESC ' 
 exec sp_executesql @sql 

2. Set AJAX controls

If you want to achieve no refresh, you will certainly use Ajax, first of all to put the necessary:

<asp:scriptmanager id= "ScriptManager1" runat= "Server" >
</asp:ScriptManager>

Then embed the Repeater control inside the UpdatePanel:

<asp:updatepanel id= "UpdatePanel1" runat= "Server" > <ContentTemplate> <ul> 
                  <asp:repeater id= "Repeater1" runat= "Server" > <ItemTemplate> <li> <p> <span><% #Eval ("username")%>:</span><% #Eval ("content"). T Ostring ())%></p> <p> <em> publish time: <% 
              #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 that the paging control is not unfamiliar to everyone, I will not introduce more. You must have AspNetPager.dll, then refer to it in your project and introduce this piece of code in the head:

<%@ Register assembly= "Aspnetpager" namespace= "Wuqi.webdiyer" tagprefix= "Webdiyer"%>
the paging control should then be available. Finally in the background to bind the data, but also to bind 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;//current page number int pageSize = 5;//Display the number of record bars per 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}//Get 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; }//Binding 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); Invokes the stored procedure if (dt. 
      Rows.Count > 0) {aspnetpager1.pagesize = PageSize; 
      Aspnetpager1.recordcount = GetCount (); 
      Aspnetpager1.currentpageindex = Currentpagenumber; This. Repeater1.datasource = dt. 
      DefaultView; This. 
    Repeater1.databind (); P//Paging event void Aspnetpager1_pagechanged (object sender, EventArgs e) {currentpagenumber = Aspnetpage R1. 
    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 (); 
 } 
    } 
  } 
}

So far, basically finished, although relatively simple, but related to stored procedures, Ajax, paging control and other knowledge points, or quite worthy of collection and learning, I hope this article is a good way to provide better methods, is my motivation to learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.