Reputation: 1230
I am binding an ASP Repeater from List & trying to add paging in the repeater. Each Page would have 5 Rows. when The Page is Run, 5 items are visible fine but when I click on "2" link button, nothing is visible. When I again Click on "1" the items of Page 2 are visible.
I've gone through this to implement Paging.
Here is my aspx Code:
<asp:Repeater ID="rp_Order" runat="server" OnItemDataBound="rp_Order_ItemDataBound">
<HeaderTemplate>
<table class="table table-responsive">
<tr >
<th style="width:20%;">Data</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<h5 style="font-weight:bold; "><%# Eval("NewsSubject") %></h5>
<p><%# Eval("NewsDate") %></p>
<img src="ImageHandler.ashx?newsid=<%# Eval("NewsID") %>" visible="<%#Eval("ImageAttachment")!=null && Eval("ImageAttachment").ToString()!=""? "true": "false" %>" class="images image-responsive myimg" />
<p ><%# System.Web.HttpUtility.HtmlDecode(Eval("NewsDescription").ToString()) %></p>
<iframe src="<%#GetUrl(Eval("youtubeurl").ToString()) %>" width="400" frameborder="0" visible="<%#Eval("youtubeurl")!=null? "true": "false" %>'"></iframe>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<asp:LinkButton ID="lnkPage"
Style="padding: 8px; margin: 2px; background: lightgray; border: solid 1px #666; color: black; font-weight: bold"
CommandName="Page" CommandArgument="<%# Container.DataItem %>" runat="server" Font-Bold="True"><%# Container.DataItem %>
</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
Here is my Cs Code:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
public void BindData()
{
List<tblLatestNew> lst_news = new List<tblLatestNew>();
lst_news = obj_news.GetLatestNews();
DataTable dtData = Utilities.ToDataTable(lst_news);
PagedDataSource pdsData = new PagedDataSource();
DataView dv = new DataView(dtData);
pdsData.DataSource = dv;
pdsData.AllowPaging = true;
pdsData.PageSize = 5;
if (ViewState["PageNumber"] != null)
pdsData.CurrentPageIndex = Convert.ToInt32(ViewState["PageNumber"]);
else
pdsData.CurrentPageIndex = 0;
if (pdsData.PageCount > 1)
{
Repeater1.Visible = true;
ArrayList alPages = new ArrayList();
for (int i = 1; i <= pdsData.PageCount; i++)
alPages.Add((i).ToString());
Repeater1.DataSource = alPages;
Repeater1.DataBind();
}
else
{
Repeater1.Visible = false;
}
rp_Order.DataSource = pdsData;
rp_Order.DataBind();
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
ViewState["PageNumber"] = Convert.ToInt32(e.CommandArgument);
BindData();
}
Please Help me resolving this! Currently my table has 7 Rows, so Page 1 should have 5 rows and Page 2 must have 2 Rows
Thanks in Advance
Upvotes: 0
Views: 619
Reputation: 56
The PagedDataSource requires a page index, which starts at zero for page 1. When you change the page in Repeater1_ItemCommand
the command argument is the page number, not the page index.
To get the page index just change your code in Repeater1_ItemCommand
so that you subtract 1 from whatever number is passed in via e.CommandArgument
, that way the proper page will display
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
ViewState["PageNumber"] = Convert.ToInt32(e.CommandArgument) - 1;
BindData();
}
Upvotes: 1