Repeater binds the database, uses AspNetPager for paging, and repeater binds the Array
Pagination is a common feature on the Web. pagination mainly includes real pages and false pages.
The so-called real paging refers to the amount of data displayed on each page that is read from the database;
False paging refers to one-time reading of all data from the database and then paging.
The difference between the two paging methods is that the method of reading information from the database is highly efficient. Fake pages are slow when the first page is loaded (if the data volume is large ).
Next we will learn how to use AspNetPager for real paging.
1. compile the data presented by Repeater in the foreground:
<Table width = "650" border = "1"> <tr> <td class = "tr1"> <asp: label Text = "name" runat = "server"> </asp: Label> </td> <td class = "tr2"> <asp: label Text = "your company" runat = "server"> </asp: Label> </td> <td class = "tr3"> <asp: label Text = "registration ID" runat = "server"> </asp: Label> </td> </tr> </table> <asp: repeater ID = "Repeater1" runat = "server"> <ItemTemplate> <table border = "1" width = "650"> <tr> <td class = "tr1"> <% # Eval ("E_Id ") %> </td> <td class = "tr2"> <% # Eval ("C_Id ") %> </td> <td class = "tr3"> <% # Eval ("User_Id ") %> </td> </tr> </table> </ItemTemplate> </asp: Repeater>Aspx
2. Add the AspNetPager Control
<Webdiyer: AspNetPager ID = "AspNetPager1" runat = "server" AlwaysShow = "true" // always display the paging control, even if the URL is divided into one page, the page information NumericButtonTextFormatString = "[{0}]" // The index format ShowCustomInfoSection = "Left" // displays the current page total page number, the default value is not displayed. left is displayed before the page index, if it is right, ShowInputBox = "Always" after the page index // input box TextAfterInputBox = "page" // TextBeforeInputBox = "Jump to section"> // before the input box </webdiyer: aspNetPager>
3. Background paging and Data Binding
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindRepeater(); } }
A. BindRepeater () function, database binding, and other operations
Public void BindRepeater () {this. AspNetPager1.PageSize = 5; // The page size int count = 1; // The default value of the current page is 1 if (Request. QueryString ["page"]! = Null) // if the current page is not empty, the page in the URL is assigned the value of {count = Convert. toInt32 (Request. queryString ["page"]. toString (); // use the URL to pass paging information. (If you use the PageChanged function of AspNetPager, The PageChanged function is called twice in the function, which affects the running efficiency )} int num = (count-1) * this. aspNetPager1.PageSize; // subtract 1 from the current page, multiply by the number displayed on each page, and obtain the number of data on the first few pages. string SQL = "select top" + this. aspNetPager1.PageSize + "* from Emp where E_Id not in (" + "select top" + num + "E_Id from Emp order by E_Id asc) order by E_Id asc "; // customize the SQL statement to find the data int recordcount on the current page; DataSet ds = GetPage (SQL, this. aspNetPager1.CurrentPageIndex, this. aspNetPager1.PageSize, out recordcount); this. aspNetPager1.RecordCount = recordcount; Repeater1.DataSource = ds; Repeater1.DataBind (); AspNetPager1.CustomInfoHTML = "Total number of records: <B>" + AspNetPager1.RecordCount. toString () + "</B>"; AspNetPager1.CustomInfoHTML + = "total page number: <B>" + AspNetPager1.PageCount. toString () + "</B>"; AspNetPager1.CustomInfoHTML + = "Current page: <font color = \ "red \"> <B> "+ count +" </B> </font> ";}
B. The GetPage function returns the dataset.
/// <Summary> /// obtain the data source /// </summary> /// <param name = "SQL"> SQL statement </param> /// <param name = "currentPage"> current page </param> /// <param name = "pagesize"> page size </param> /// <param name = "recordcount"> total number of pages </param> /// <returns> DataSet </returns> public DataSet GetPage (string SQL, int currentPage, int pagesize, out int recordcount) {// String strSql = "select * from Emp"; SqlDataAdapter ada = new SqlDataAdapter (SQL, GetConnection ()); dataSet ds = new DataSet (); // int startRow = (currentPage-1) * pagesize; // ada. fill (ds, startRow, pagesize, "table"); // pagination is performed on the read data. This operation can be performed on false pages in ada. fill (ds, "table"); // Fill in recordcount = GetPageRecord (); // get the total number of pages return ds ;}
C. GetPagRecord function to obtain the total number of records
/// <Summary> /// obtain the total number of records /// </summary> /// <param name = "SQL"> </param> /// <returns> </returns> public int GetPageRecord () {String SQL = "select count (*) from Emp"; SqlCommand cmd = new SqlCommand (SQL, GetConnection (); cmd. connection. open (); int recordcount = (int) cmd. executeScalar (); return recordcount ;}
D. GetConnection to obtain the connection string
public SqlConnection GetConnection() { SqlConnection conn = new SqlConnection("server=.;database=ComInfo;integrated security=true"); return conn; }
How to Use the AspNetPager space? The data-bound control uses Repeater.
In the first line, you must reference AspNetPager. dll and add the following code to system. web in web. config to register AspNetPager:
<System. web>
<Pages>
<Controls>
<Add tagPrefix = "divPager" namespace = "Wuqi. Webdiyer" assembly = "AspNetPager"/>
</Controls>
</Pages>
</System. web>
Then use the following in the page:
<DivPager: AspNetPager ID = "pager" OnPageChanging = "pager_PageChanging" runat = "server"> </divPager: AspNetPager>
The style must be modified by yourself. In cs, protected void pager_PageChanging (object src, PageChangingEventArgs e) must be implemented)
That's all. If you still don't understand it, ask me.
Use of the Repeater control and the AspNetPager control in ASPNET
1. First, you need to check the total number of rows.
2. The first five messages are sent to AspNetPager (top 5 in SQL)
3. Write paging information in the AspNetPager event. In the SQL statement, the id to be written is not in the data to be displayed.