Wednesday, 16 January 2013 13:56

how to implement paging for datalist control

Written by 
Rate this item
(0 votes)

I wanted paging for showing datalist items in pages.for
that i used pageddatasource.it's necessary very much.
following codes show how to add paging for datalist items -

first add two buttons in aspx page -

<asp:LinkButton ID="Prev" OnClick="Prev_Click" runat="server" Text="Previous" />
<asp:LinkButton ID="Next" OnClick="Next_Click" runat="server" Text="Next" />

then in code behind file -

List<items> itemsList = new List<items>();
MS_items_Controller control = new MS_items_Controller();
itemsList = control.MS_Comments_LoadAllItems(1, 1);

PagedDataSource pages = new PagedDataSource();
pages.AllowPaging = true;
pages.PageSize =10;//size of your page

pages.CurrentPageIndex = CurrentPage;
pages.DataSource = itemsList;
Prev.Enabled = !pages.IsFirstPage;
Next.Enabled = !pages.IsLastPage;
DataList1.DataSource = pages;
DataList1.DataBind();

public int CurrentPage
{
get
{
object o = this.ViewState["_CurrentPage"];
if (o == null)return 0;
else return (int)o;
}
set
{this.ViewState["_CurrentPage"] = value;}
}
protected void Prev_Click(object sender, System.EventArgs e)
{
// int u=Convert.ToInt32(ViewState["CurrentPage2"]);
//CurrentPage -= 1;
CurrentPage -= 1;
// Reload
bindgrid();
}
protected void Next_Click(object sender, System.EventArgs e)
{
CurrentPage += 1;
// Reload
bindgrid();
}

Read 2275 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.
Login to post comments