Paging of Data list
When you have datalist there is one main problem in it and that is paging.. because paging is not allowed in data list.. You can solve this problem using this method...
On .aspx page.
we have create datalist with images.
<asp:DataList runat="server" id="datalist1" RepeatColumns="4" RepeatDirection="Horizontal">
<ItemTemplate>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<img src="<%=DataBinder.Eval(Container.DataItem,"Image_name")%>" width="90" height="90">
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
we have take two linkbutton for next and previous button for using paging..
<table border="0" width="410">
<tr>
<td align="left">
<asp:LinkButton ID="lbPrev" Runat="server">
Prev
</asp:LinkButton>
</td>
<td align="right">
<asp:LinkButton ID="lbNext" Runat="server">
Next
</asp:LinkButton>
</td>
</tr>
</table>
on .aspx.cs page...
binddatalist()
{
// use PageDatasource object for paging purpose..
PagedDataSource Page = new PagedDataSource();
try
{
DataSet ds = ds // ds is your datasource..
Page.AllowPaging = true;
//page object store the datasource of datalist..
Page.DataSource = ds.Tables[0].DefaultView;
Page.PageSize = 8;
//"CurrentPage" is global variable that content the current page index..
Page.CurrentPageIndex = CurrentPage;
dlGallery.DataSource = Page;
dlGallery.DataKeyField = "Image_ID";
dlGallery.DataBind();
//now datalist only show the a images...
//due to "Page.PageSize = 8;" statement...
//visible true or false of next and previous button
//according to last and first page...
lbtnNext.Enabled = !objPage.IsLastPage;
lbtnPrev.Enabled = !objPage.IsFirstPage;
}
catch(Exception ex)
{
throw ex;
}
}
then we have maintain paging using link and next button as below...
private void lbPrev_Click(object sender, System.EventArgs e)
{
CurrentPage -=1;
//"CurrentPage" is global variable that content the current page index..
BindList();
}
private void lbNext_Click(object sender, System.EventArgs e)
{
CurrentPage +=1;
//"CurrentPage" is global variable that content the current page index..
BindList();
}
Now your paging will be solve...
regards
varun bansal
Hey i want to use your code for paging but i few thing are missing
dlGallery=?
objPage=?
BindList()?
Where are the defination of there variable and functions, please attach working code if possible