Asp. NET paging component learning and using--teaching articles

Source: Internet
Author: User
Tags count min tostring client
asp.net| paging ASP. NET paging component learning and using--teaching articles



No one doubts the role of the paging component in the Web application. The number of records in the database is tens of thousands or even billions, if the peremptorily display on a page is clearly unrealistic, such programmers are also too small pediatrics. So, the best way is to page display, each page only shows a part of the record in the database, you can page, you can enter a page number to the specified page, this is the current more common usage.



The difference of this article is that I encapsulate the functionality of pagination in the component, which embodies the object-oriented features, on the other hand, it is easy to publish, share and use. In advance, this article no longer describes the detailed process of component creation, if there is doubt please refer to the other related articles of this blog (asp.net component design, asp.net component programming step by Step).



Let's look at the appearance of the component first:








The final run of the component displayed on the client is actually a table, the table is divided into three paragraphs, the first paragraph is related to the page and record information; the second section is pager, which shows a set of numbers with hyperlinks, which can be transferred to the corresponding page by clicking the number, and the third segment has two HTML controls, The user can enter a number to transfer to the specified page. As you can see from the diagram, the functionality of this component is simple and straightforward.



First, let's focus on the first part. This includes the current page, the total number of pages, the number of record bars displayed per page, and the total record bar count. This information is passed from outside the component, so we define the corresponding attributes:

private int _count;//The number of record bars displayed per page

private int _currentpage;//Current page

private int _allcount;//All of the record bars

private int _showpages;//Display number

I'm a bit obscure in commenting _showpages this attribute, so I need to simply say this: The attribute is used to define the number of digits displayed in the digital navigation bar, in the figure above, the definition shows 10 digits, from 201--210, of course, we can define as many numbers as needed.



[DefaultValue, Category ("Customer")]

public int Count

{

Set

{

if (value <= 0)

_count = 1;

Else

_count = value;

}

Get

{

return _count;

}

}



[DefaultValue (1), Category ("Customer")]

public int CurrentPage

{

Set

{

if (Value < 0)

_currentpage = 1;

Else

_currentpage = value;

}

Get

{

return _currentpage;

}

}



[DefaultValue (1), Category ("Customer")]

public int Allcount

{

Get

{

return _allcount;

}

Set

{

if (_allcount < 0)

throw new Exception ("record total number of bars cannot be negative");

Else

_allcount = value;

}

}



[DefaultValue (1), Category ("Customer")]

public int pages//Total pages

{

Get

{

if (this._allcount% This._count > 0)

return ((int) this._allcount/this._count) + 1;

Else

return ((int) this._allcount/this._count);

}

}



[DefaultValue (1), Category ("Customer")]

public int Showpages

{

Set

{

if (Value > 0)

_showpages = value;

Else

_showpages = 9;

}

Get

{

return _showpages;

}

}



In the defined attribute, there is a property called pages that does not need to pass the value from the outside, but is computed. His value is equal to the total number of record bars divided by the number of records displayed per page (see Code for details).



Now we're going to show these values and display them in the following code:

The page bar is divided into three parts, Leftinfo is the leftmost part, used to display the current page/total pages, the number of records displayed per page

Leftinfo = "page:" + this. Currentpage.tostring () + "/" + this. Pages.tostring () + "" + "per page" + this. Count.tostring () + "strip" + "total" + this. Allcount.tostring () + "strip";



The second paragraph is more complex. The page navigation number for the component is continuous, so I define a minimum and maximum value:

int min;//The minimum number of page navigation values to display

int max;//Maximum number of page navigation values to display



At design time, three scenarios need to be considered:

1: If the current page is divided by the showpages remainder of 0, which is exactly divisible, the page navigation number minimum and maximum values are:

Min min value = Current page + 1

Max max = Current page + number of page navigation numbers (showpages)

Corresponding Code:

if (this. CurrentPage% this. Showpages = = 0)//if exactly divisible

{

Min = this. CurrentPage + 1;

Max = this. CurrentPage + this. Showpages;

}

2: If the current page is the minimum navigation number, you should switch to the previous set of navigation numbers. At this point, the minimum and maximum values for the navigation numbers are:

Min min value = ((int) current page/page navigation number Showpages)-1 * page navigation number showpages +1;

Max max = Current page –1

The corresponding code is as follows:

else if (this. CurrentPage% this. Showpages = = 1 && this. CurrentPage > this. Showpages)

{

min = ((int) this. Currentpage/this. Showpages)-1) * this. Showpages +1;

Max = this. CurrentPage-1;

}



3: If the current page is the maximum navigation number, you should switch to the last set of navigation numbers. At this point, the minimum and maximum values for the navigation numbers are:

Min min value = ((int) current page/page navigation number showpages) * Page navigation number Showpages + 1

Max max = ((int) current page/page navigation number Showpages) + 1) * Number of page navigation numbers showpages

The corresponding code is as follows:

Else

{

min = (int) this. Currentpage/this. Showpages) * this. Showpages + 1;

max = ((int) this. Currentpage/this. Showpages) + 1) * this. Showpages;

}



That is, the minimum and maximum values of the navigation number list are calculated, so we do a circular operation to generate the navigation, the current page is highlighted in italics and red fonts:

for (int i = min; I <= max; i++)

{

if (i <= this. Pages)//is only displayed if it is not greater than the maximum page

{

if (this. CurrentPage = i)//if the current page, with italic and red display

{

Numberstr = Numberstr + "<a href=" + Absurl + "? currentpage=" + i.tostring () + ">" + "<i style= ' color:red ' >" + i.ToString () + "</I>" + "</a>" + "\ n";

}

Else

{

Numberstr = Numberstr + "<a href=" + Absurl + "? currentpage=" + i.tostring () + ">" + i.tostring () + "</a>" + "\ n ";

}

}

}

As you can see, there are four more icons in the front and back of the navigation list, and these icons are not pictures, but wedding fonts of 73,484 digits. The code for these four icons is as follows:

First page, previous page, next page, last page

String First,previous,next,last;

The Absurl + "currentpage=1";

/////////

if (this. CurrentPage = 1)

Previous = Absurl + "? currentpage=1";

Else

Previous = Absurl + "? currentpage=" + (this. CURRENTPAGE-1). ToString ();

/////////

if (this. CurrentPage = = this. Pages)

Next = Absurl + "? currentpage=" + this. Pages;

Else

Next = Absurl + "? currentpage=" + (this. CurrentPage + 1). ToString ();

/////////

Last = Absurl + "? currentpage=" + this. Pages;



The next code is to generate the HTML string to be exported to the client:

Centerinfo.appendformat ("<font face= ' webdings ' style= ' font-size:14px ') ><a href={0}>7</a><a Href={1}>3</a></font>{2}<font face= ' webdings ' style= ' font-size:14px ' ><a href={3}>4< /a><a href={4}>8</a></font> ", first,previous,numberstr,next,last);



StringBuilder sb = new StringBuilder ();//html string

Sb. AppendFormat ("<table style = ' font-size:12px ' border= ' 0 ' cellpadding= ' 0 ' cellspacing= ' 0 ' width= ' 100% ' > \ n" +

"<tr>\n" +

"<td width= ' 25% ' align= ' left ' >{0}</td>\n" +

"<td width= ' 61% ' align= ' right ' >{1}</td>\n" +

"<td width= ' 14% ' align= ' right ' ><input type= ' text ' name= ' T1 '" size= ' 4 ' style= ' Border-bottom:solid ' 1pt gray; Border-top:solid 1pt Gray; Border-left:solid 1pt gray;border-right:solid 1pt Gray; ' > \ <input type= ' button ' name= ' B1 ' size= ' 6 ' value=go style= ' border-bottom:solid 1pt gray;border-top:solid 1pt Gray ; Border-left:solid 1pt gray;border-right:solid 1pt Gray; ');

if (index = = 1)

{

}

Else

{

url = url.substring (0,index);

}

Location.href = URL + "? currentpage=" + Ctrl.value;



}

Else

{

Alert ("The page number you enter must be the number that meets the page requirements, the maximum is:" + max);

return false;

}

}

</script>

Parameter description: Ctrl is the text box, Max is the maximum value entered, that is, the total number of pages.



Rewrite the OnPreRender () method to enter the script into the browser:

protected override void OnPreRender (EventArgs e)

{

Base. OnPreRender (e);

if (! Page.isclientscriptblockregistered ("WEREW-332DFAF-FDAFDSFDSAFD"))

{Page.registerclientscriptblock ("werew-332dfaf-fdafdsfdsafd", scriptstring);

}

}

Finally finished, you understand? Please forgive me for the place where I have not lied.

The next article is: ASP. NET paging components learning and using--use of the article, please pay attention!





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.