While using the gridView and listView it is an easier pratice to use the data pager control for pagging purpose.
But while you are working with datalist control then you will face the real problem, because the datalist does not support the data pager.
In order to impliment the paging functionalty in data list here is the simplest way.
Take two linkbuttons below the data list, named Previous and Next as below.
Now in the code behind declare the " ViewState["currentPage"] = 0; " in page load event. Then do as I did in the BindDataList() method. ViewState["currentPage"] = 0; protected void BindDataList() { DataTable dt = newDataTable(); dt = null // assign the datatable with what you want to assign. PagedDataSource page = newPagedDataSource(); page.AllowPaging = true; page.DataSource = dt.DefaultView; page.PageSize = 6; page.CurrentPageIndex = Convert.ToInt32(ViewState["currentPage"]); dlLogoImages.DataSource = page; dlLogoImages.DataBind(); lbtnNext.Enabled = !page.IsLastPage; // Disable the next button if the page is the last page. lbtnPrevious.Enabled = !page.IsFirstPage; // Disable the Previuos button if the page is the first page. }
After doing all these things then do as below in the onclick events of the two buttons.
protectedvoid lbtnPrevious_onClick(object sender, EventArgs e) { int currentPage = Convert.ToInt32(ViewState["currentPage"]); currentPage -= 1; ViewState["currentPage"] = currentPage; BindDataList(); } protectedvoid lbtnNext_onClick(object sender, EventArgs e) { int currentPage = Convert.ToInt32(ViewState["currentPage"]); currentPage += 1; ViewState["currentPage"] = currentPage; BindDataList(); }
Now you are done with the paging in DataList.
protectedvoid lbtnPrevious_onClick(object sender, EventArgs e) { int currentPage = Convert.ToInt32(ViewState["currentPage"]); currentPage -= 1; ViewState["currentPage"] = currentPage; BindDataList(); } protectedvoid lbtnNext_onClick(object sender, EventArgs e) { int currentPage = Convert.ToInt32(ViewState["currentPage"]); currentPage += 1; ViewState["currentPage"] = currentPage; BindDataList(); }
Now you are done with the paging in DataList.