A digital paging Control

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Web. UI. webcontrols;
Using system. Web. UI;
Using system. componentmodel;

Namespace sky_web.common
{
// Digital paging Control
[Toolboxdata ("<{0}: sky_page runat = Server> </{0}: sky_page>")]
Public class sky_page: webcontrol, ipostbackeventhandler
{
Private int pageindex = 0; // index of the current page
Private int pagesize = 10; // number of entries per page
Private int pagecount = 0; // total number of pages
Private int showpages = 8; // Number of displayed pages

[Category ("Total number of pages")]
Public int pagecount
{
Set
{
If (value <0)
PageCount = 0;
Else
PageCount = value;
}
Get {return pageCount ;}
}
[Category ("Number of displayed pages")]
Public int ShowPages
{
Set
{
If (value <= 0)
Showpages = 8;
Else
Showpages = value;
}
Get {return showpages ;}
}
[Category ("number of entries per page")]
Public int pagesize
{
Set
{
If (value <= 0)
Pagesize = 10;
Else
Pagesize = value;
}
Get {return pagesize ;}
}
[Category ("Current page index")]
Public int pageindex
{
Set
{
If (value <= 0)
Pageindex = 0;
Else
Pageindex = value;
}
Get {return pageindex ;}
}

Private Static readonly object eventcommand = new object ();
Public event commandeventhandler command
{
Add
{
Base. Events. addhandler (eventcommand, value );
}
Remove
{
Base. Events. removehandler (eventcommand, value );
}
}

Protected virtual void oncommand (commandeventargs E)
{
Commandeventhandler handler = (commandeventhandler) base. events [eventcommand];
If (handler! = NULL)
{
Handler (this, e );
}
}
// Display the page
Protected override void render (htmltextwriter output)
{
If (pageindex <0 | pageindex> pagecount-1)
Pageindex = 0;

Output. Write ("<Table width =/" 100%/"border =/" 0/"cellpadding =/" 0/"cellspacing =/" 0/"> ");
Output. Write ("<tr> ");
Output. Write ("<TD align =/" right/"> ");
Output. write ("<input id = 'button1' type = 'button 'style = 'border: 0; Background-color: White; text-align: center; Vertical-align: bottom '");
Output. Write ("value = 'homepage' onclick =/" "+ getaction (0) +"/"/> ");
Output. Write ("If (pageIndex> 0) // you can also go up
Output. Write ("style = 'cursor: pointer 'onclick =/" "+ getAction (pageIndex-1) + "/"");
Output. Write ("> ");
// Find the maximum and minimum indexes to be displayed
Int maxIndex = pageIndex;
Int minindex = pageindex;
Showpages --; // remove the current page
Bool notify;
While (showpages> 0)
{
Notify = true;
If (maxindex + 1 <pagecount & showpages> 0) // find a page forward
{
Maxindex ++;
Showpages --;
Notify = false;
}
If (minindex-1> = 0 & showpages> 0) // locate a page
{
Minindex --;
Showpages --;
Notify = false;
}
If (notify) // No search is found, indicating the total number of showpages>
Break;
}
For (INT Index = minindex; index <= maxindex; index ++) // display the page number
{
If (index! = Pageindex)
Output. write ("<a href =/" "+ getaction (INDEX) +"/">" + (index + 1 ). tostring () + "</a> ");
Else
Output. Write (index + 1). ToString () + "");
}
Output. Write ("If (pageIndex <pageCount-1) // you can go down
Output. Write ("style = 'cursor: pointer 'onclick =/" "+ getAction (pageIndex + 1) + "/"");
Output. Write ("> </td> ");
Output. Write ("<td style = 'align: left; '> ");
Output. write ("<input id = 'button2' type = 'button 'style = 'border: 0; background-color: White; text-align: left; vertical-align: bottom '");
Output. Write ("value = 'last page' onclick =/" "+ getAction (pageCount-1) +"/"/> ");
Output. Write ("Total" + pageCount. ToString () + "page ");
Output. Write ("</td> </tr> </table> ");
}

Private string getAction (int index)
{
Return "javascript:" + this. Page. ClientScript. GetPostBackEventReference (this, index. ToString ());
}

# Region IPostBackEventHandler Member

Void IPostBackEventHandler. RaisePostBackEvent (string eventArgument)
{
This. OnCommand (new CommandEventArgs ("", eventArgument ));
}

# Endregion
}
}

Call the. cs page:

Protected void sky_pageappscommand (object sender, CommandEventArgs e)
{

Int index = Convert. ToInt32 (e. CommandArgument );
Sky_Page1.PageSize = 10;
Sky_Page1.PageCount = 8;
Sky_Page1.ShowPages = 8;
Sky_Page1.PageIndex = index;

ViewState ["pageIndex"] = index;
This. Repeater1.DataSource = GetDs (10, index + 1). Tables [0]. DefaultView;
This. Repeater1.DataBind ();

}

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.