Use the pageddatasource class to display datalist and repeater controls by page.

Source: Internet
Author: User

Asp.net provides three powerful list controls: the DataGrid, datalist, and repeater controls, which have higher style customization compared with the DataGrid, datalist, and repeater controls, many times we like to use the datalist or Repeater control to display data. However, repeater and datalist do not have the paging function and are sometimes inconvenient.

The pageddatasource class encapsulates the properties of the DataGrid control so that the DataGrid Control can execute pagination. It is a data container. We first read the data from the database and put it in this container, then, set the container property to retrieve part of the data on the page to be displayed, and then bind the data to the display control on the page.

The following example shows the data paging list implemented by the Repeater control and hyperlink control combined with the pageddatasource class.


Data function implementationSource code:

 

Paging code
// Reference to the class used for paging
Pageddatasource PPS = New Pageddatasource ();
PPS. datasource = DT. defaultview; // Set the data source (datatable type)
PPS. allowpaging = True ;
// Number of lines displayed on the page
PPS. pagesize = 18 ;

// Set current page
If (Pageindex < 1 ) Pageindex = 1 ;
PPS. currentpageindex = Pageindex - 1 ;

Rpt_newslist.datasource = PPS;
Rpt_newslist.databind ();

// Display page number
Ltl_recordcount.text = PPS. performancecount. tostring ();
Ltl_pagecount.text = PPS. pagecount. tostring ();
Ltl_pageindex.text = Pageindex. tostring ();
Ltl_jump.text = Jump_list (PPS. pagecount, pageindex, l_manage );

// Display up and down pages (the parameters following the URL are defined as needed)
Lbn_first.tooltip =   " Go to homepage " ;
Lbn_first.navigateurl = Request. currentexecutionfilepath + " ? Org_id = " + Rochelle manage + " & Page = 1 " ;
Lbn_prev.tooltip =   " Jump to previous page " ;
Lbn_prev.navigateurl = Request. currentexecutionfilepath + " ? Org_id = " + Rochelle manage + " & Page = " + (Pageindex - 1 );
Lbn_next.tooltip =   " Jump to next page " ;
Lbn_next.navigateurl = Request. currentexecutionfilepath + " ? Org_id = " + Rochelle manage + " & Page = " + (Pageindex + 1 );
Lbn_last.tooltip =   " Jump to the last page " ;
Lbn_last.navigateurl = Request. currentexecutionfilepath + " ? Org_id = " + Rochelle manage + " & Page = " + PPS. pagecount. tostring ();

// Determine the link Display Mode
If (Pageindex <= 1   && PPS. pagecount <= 1 )
{
Lbn_first.navigateurl= "";
Lbn_prev.navigateurl= "";
Lbn_next.navigateurl= "";
Lbn_last.navigateurl= "";
}
If (Pageindex <= 1   && PPS. pagecount > 1 )
{
Lbn_first.navigateurl= "";
Lbn_prev.navigateurl= "";
}
If (Pageindex > = PPS. pagecount)
{
Lbn_next.navigateurl= "";
Lbn_last.navigateurl= "";
}

/**/ ///
// calculate paging jump
//
// page count
// string
Private   String Jump_list ( Int Pagecount, Int Pageindex, Long Rochelle manage)
{
Stringbuilder sb =   New Stringbuilder ();
SB. append ( " <Select id = \ " Page_jump \ " Name = \ " Page_jump \ " Onchange = \ " Window. Location = ' "+ Request. currentexecutionfilepath + "? Page = ' +   This . Options [ This . Selectedindex]. Value +   ' & Org_id = "+ l_manage +" ' ;\ " > " );
For ( Int I =   1 ; I <= Pagecount; I ++ )
{
If (Pageindex = I)
SB. append ( " <Option value =' " + I + " 'Selected> " + I + " </Option> " );
Else
SB. append ( " <Option value =' " + I + " '> " + I + " </Option> " );
}
SB. append ( " </SELECT> " );

Return SB. tostring ();
}

 

 

Some public attributes of the pageddatasource class:
Allowcustompaging gets or sets the value indicating whether to enable custom paging.
Allowpaging gets or sets the value indicating whether to enable pagination.
Count to obtain the number of items to be used from the data source.
Currentpageindex gets or sets the index of the current page.
Datasource obtains or sets the data source.
Cececount gets the number of items in the data source.
Firstindexinpage gets the first index on the page.
Iscustompagingenabled gets a value indicating whether to enable custom paging.
Isfirstpage gets a value indicating whether the current page is a homepage.
Islastpage gets a value indicating whether the current page is the last page.
Ispagingenabled gets a value indicating whether to enable paging.
Isreadonly gets a value indicating whether the data source is read-only.
Issynchronized gets a value indicating whether to synchronize access to the data source (thread safety ).
Pagecount obtains the total number of pages required for all items in the data source.
Pagesize gets or sets the number of items to be displayed on a single page.
Virtualcount gets or sets the actual number of items in the data source when custom pages are used.

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.