Pagination | Control DataList controls are powerful, he supports selection, editing, and implementation is simple, but the most frustrating thing is that it does not have the ability to page pagination like the DataGrid control, so a good control cannot be paged!!! It's a really frustrating thing to do.
However, just DataList does not provide the built-in paging functionality, but does not mean that we can not use the DataList control to implement pagination, since it does not give me paging functionality, it had to do it myself.
The following is all the original code, in fact, the method used in PHP and the page is similar, but here is the combination of DataAdapter and dataset, rather than PHP in the SQL statement directly to fix.
Note: The command event for the two LinkButton control must be changed to Page_onclick:
Codebehind file:
private void Page_Load (object sender, System.EventArgs e)
{
pagesize=1;
Conn= New SqlConnection (application["Guest_conn"). ToString ());
Conn.Open ();
if (! Page.IsPostBack)
{
Listbind ();
currentpage = 0;
viewstate["PageIndex"] = 0;
Calculate how many records are in total
RecordCount = Calculaterecord ();
Lblrecordcount.text = Recordcount.tostring ();
Calculate how many pages are in total
PageCount = recordcount/pagesize;
Lblpagecount.text = Pagecount.tostring ();
viewstate["PageCount"] = PageCount;
}
}
Calculate how many records are in total
public int Calculaterecord ()
{
Int intcount;
String strcount = "SELECT count (*) as Co from guest";
SqlCommand Mycomm = new SqlCommand (strcount,conn);
SqlDataReader dr = Mycomm.executereader ();
if (Dr. Read ())
{
intcount = Int32.Parse (dr["co"). ToString ());
}
Else
{
intcount = 0;
}
Dr. Close ();
Return intcount;
}
ICollection Createsource ()
{
int StartIndex;
Set the start address of the import
StartIndex = currentpage*pagesize;
String Strsel = "SELECT * from guest ORDER by postdate Desc";
DataSet ds = new DataSet ();
SqlDataAdapter myadapter = new SqlDataAdapter (Strsel,conn);
Myadapter.fill (Ds,startindex,pagesize, "Guest");
Return DS. tables["Guest". DefaultView;
}
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.