ASP. NET tips: Create a datalist paging Data Source

Source: Internet
Author: User

This data source is divided into two parts. One is to call up data from the data class, and then operate the paging parameters and page paging auxiliary controls in this data source! There are three controls in front to control flip pages, one drop-down list and two linkbuttons!

The following fill () method is easy to call. You can simply rebind fill () on the page! But it must be written, for example, after the page turning action is executed!

The ds method in the DB class is as follows:

Public static datatable DS (string que)
{// Return a data table loaded with an SQL-based message,
Oledbconnection con = ODB. Con ();
Oledbdataadapter ODA = new oledbdataadapter ();
ODA. selectcommand = new oledbcommand (que, con );
Dataset DS = new dataset ();
ODA. Fill (DS, "thc ");
Return Ds. Tables ["thc"];
Con. Close ();
} The data source used in the following method is
Private void fill ()
{// Method, because there will be multiple bindings in the page
// Set a hidden label to store the current page index.
Int cup = convert. toint32 (pagelbl. Text );

Pageddatasource PS = new pageddatasource (); // a new paging Data Source
PS. datasource = ODB. DS ("select * From guest order by id desc "). defaultview; // send an SQL statement to confirm the data source of the data source.
PS. allowpaging = true; // allow pagination
PS. pagesize = 2; // set the number of pages
PS. currentpageindex = Cup-1;
If (! Ispostback)
{// Determine whether the page is loaded for the first time
For (INT I = 1; I <= ps. pagecount; I ++)
{// Loop page number
Pageddl. Items. Add (I. tostring ());
}
}
// The following mainly controls whether the up/down page button is used
Pageup. Enabled = true;
Pagedown. Enabled = true;
If (PS. isfirstpage)
{// If it is the first page, the previous page is unavailable
Pageup. Enabled = false;
}
If (PS. islastpage)
{// If it is the last page, the next page is unavailable
Pagedown. Enabled = false;
}
// Set the selected value in the page number drop-down menu
Pageddl. selecteditem. Text = Cup. tostring ();
// You can finally bind it to datalist.
Datalist1.datasource = Ps;
Datalist1.datakeyfield = "ID ";
Datalist1.databind ();
}

Next is how to handle page flip events.

Protected void pageddl_selectedindexchanged (Object sender, eventargs E)
{// Page number drop-down menu event
Pagelbl. Text = pageddl. selecteditem. Text. tostring ();
Fill ();
}
Protected void pagedown_click (Object sender, eventargs E)
{// Next page event
Pagelbl. Text = convert. tostring (convert. toint32 (pagelbl. Text) + 1 );
Fill ();
}
Protected void pageup_click (Object sender, eventargs E)
{// Previous event
Pagelbl. Text = convert. tostring (convert. toint32 (pagelbl. Text)-1 );
Fill ();
}

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.