Asp.net uses the background to generate html pagination directly. asp.net pagination

Source: Internet
Author: User
Tags asp net

Asp.net uses the background to generate html pagination directly. asp.net pagination

This example describes how to use asp.net to generate html pagination directly in the background. It is a practical function. Share it with you for your reference. The specific method is as follows:

1. Create a stored procedure:

ALTER procedure [dbo].[p_news_query]@Page intasbeginselect top 5 new_id,new_title,new_url,new_content_text,create_time,user_name from  (select *,ROW_NUMBER() over(order by new_id desc ) as RowNumber  from    (select new_id,new_title,new_url,new_content_text,        dbo.f_ConvertDate(a.create_time) create_time, b.user_name from xs_new a        left join xs_users b on b.user_no=a.create_user)TI ) A  where A.RowNumber>(@Page-1)*5end

Currently, the experiment has five data entries per page. You can input the current page number.

2. Define a div in the foreground for display

 <div id="divPage" runat="server" class="divpage">  </div>

3. search html in the background based on input parameters

At present, it is an imitation of the blog garden mode. There are 5 items displayed before and after, and the redundant addition... is followed by the last one. The Code is as follows:

StringBuilder sbr = new StringBuilder (); int ITotalCount = pageLogic. QueryNewsCount (); int IPage = 1; if (Request ["p"]! = Null) {IPage = Convert. toInt32 (Request ["p"]);} int IPageCount = 5; int ITotalPage = ITotalCount/IPageCount; if (ITotalCount % IPageCount> 0) {ITotalPage + = 1 ;} if (IPage! = 1) {sbr. Append ("<a href = \" news. aspx? P = "+ (IPage-1 ). toString () + "\"> previous page </a> ");} if (ITotalPage <= 10) {for (int I = 1; I <= ITotalPage; I ++) {if (I = IPage) {sbr. append ("<a href = \" news. aspx? P = "+ I. toString () + "\" class = \ "pageSelect \"> "+ I. toString () + "</a>");} else {sbr. append ("<a href = \" news. aspx? P = "+ I. toString () + "\"> "+ I. toString () + "</a>") ;}} else {if (IPage-5> 2 & IPage + 7 <ITotalPage) // both front and back {sbr. append ("<a href = \" news. aspx? P = 1 \ "> 1 </a>... "); for (int I = IPage-5; I <= IPage + 5; I ++) {if (I = IPage) {sbr. append ("<a href = \" news. aspx? P = "+ I. toString () + "\" class = \ "pageSelect \"> "+ I. toString () + "</a>");} else {sbr. append ("<a href = \" news. aspx? P = "+ I. toString () + "\"> "+ I. toString () + "</a>") ;}} sbr. append ("... <a href = \ "news. aspx? P = "+ ITotalPage. toString () + "\"> "+ ITotalPage. toString () + "</a>");} else if (IPage-5 <= 2) // The front is not enough, followed by extra {for (int I = 1; I <= 10; I ++) {if (I = IPage) {sbr. append ("<a href = \" news. aspx? P = "+ I. toString () + "\" class = \ "pageSelect \"> "+ I. toString () + "</a>");} else {sbr. append ("<a href = \" news. aspx? P = "+ I. toString () + "\"> "+ I. toString () + "</a>") ;}} sbr. append ("... <a href = \ "news. aspx? P = "+ ITotalPage. toString () + "\"> "+ ITotalPage. toString () + "</a>");} else if (IPage + 7> = ITotalPage) // The front is redundant, and the back is not enough {sbr. append ("<a href = \" news. aspx? P = 1 \ "> 1 </a>... "); for (int I = ITotalPage-10; I <= ITotalPage; I ++) {if (I = IPage) {sbr. append ("<a href = \" news. aspx? P = "+ I. toString () + "\" class = \ "pageSelect \"> "+ I. toString () + "</a>");} else {sbr. append ("<a href = \" news. aspx? P = "+ I. ToString () +" \ ">" + I. ToString () + "</a>") ;}}} if (IPage! = ITotalPage) {sbr. Append ("<a href = \" news. aspx? P = "+ (IPage + 1 ). toString () + "\"> next page </a> ");} divPage. innerHtml = sbr. toString (); rptNews. dataSource = pageLogic. queryNews (IPage); rptNews. dataBind ();

4. Shows the test result:

I hope this article will help you design your asp.net program.


Asp net dynamically generates HTML pages

The simplest method is to capture the. aspx page through WebRequest on page B, save the Html code, and replace the. aspx? Page = xxx

Example code:

Int pageCount = 1; // fill in this variable when capturing the first page
// The page with hypothetical persistence is 1. htmto other htm
For (int I = 1; I <= pageCount; I ++ ){
String url = "www.abc.com/a.aspx? Page = "+ I; // loop 20 pages and capture 20 html
HttpWebRequest request = (HttpWebRequest) WebRequest. Create (url );
HttpWebResponse response = request. GetResponse () as HttpWebResponse;
Stream stream = response. GetResponseStream ();
String html;
Using (StreamReader reader = new StreamReader (stream ))
{
Html = reader. ReadToEnd ();
}

Regex reg = new Regex (@ "a \. aspx \? Page = (\ d *) ", RegexOptions. Compiled | RegexOptions. IgnoreCase );
// Fill in the page number variable when capturing the first page
If (I = 1 ){
MatchCollection mc = reg. Matchs (html );
If (mc. Count> 0 ){
Int. TryParse (mc [mc. Count-1]. Result ("$1"), out pageCount );
}
}

// Replace the link of a. aspx In the captured html to put a. aspx? Page = Replace the paging link with "page sharding .htm"
Html = reg. Replace (html, "20.1.htm ");

// Save the html captured to a static file
Using (StreamWriter sw = new StreamWriter (HttpContext. Current. Server. MapPath (I + ". htm ")))
{
Sw. Write (html );
}
}

How to Implement paging when asp net generates html static pages?

Int nRow = int. Parse (sRow); // The total number of returned data rows.
PageRecord = 10;
Int PageCount = nRow % PageRecord = 0? NRow/PageRecord: nRow/PageRecord + 1; // The total number of PageCount pages. The number of records displayed on each page of PageRecord
Int iStart = PageRecord * (Page-1); // starting from the row
Int n = iStart + PageRecord;
If (iStart <0) iStart = 0;
If (n> nRow) n = nRow;
For (int I = iStart; I <n; I ++)
{

}

Related Article

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.