Here I am showing a simple but not like so :) i.e paging of a Repeater or datalist in asp.net using C#
.aspx Content
------------------
<asp:Repeater ID="rptPosts" runat="server" OnItemDataBound="rptPosts_ItemDataBound"
OnItemCommand="rptPosts_ItemCommand">
<ItemTemplate>
<asp:Label ID="lblSubject" runat="server" Text='<%#Eval("Subject")%>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
<asp:Repeater ID="rptPages" runat="server">
<HeaderTemplate>
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
<b>Page:</b>
</td>
<td>
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="btnPage" CommandName="Page" CommandArgument="<%#
Container.DataItem %>" CssClass="f-anchor" runat="server"><%# Container.DataItem %>
</asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
</td> </tr> </table>
</FooterTemplate>
</asp:Repeater>
.cs Content
----------------
private void LoadData(int topicId)
{
lnkDelTopic.CommandArgument = Convert.ToString(topicId);
lnkEditTopic.CommandArgument = Convert.ToString(topicId);
PagedDataSource pgitems = new PagedDataSource();
pl_post objPost = new pl_post();
objPost.TopicId = topicId;
postList = bl_forum.GetPostListByTopicId(objPost);
pgitems.DataSource = postList;
pgitems.AllowPaging = true;
pgitems.PageSize = 5;
pgitems.CurrentPageIndex = PageNumber;
if (pgitems.PageCount > 1)
{
rptPages.Visible = true;
ArrayList pages = new ArrayList();
for (int i = 0; i < pgitems.PageCount; i++)
pages.Add((i + 1).ToString());
rptPages.DataSource = pages;
rptPages.DataBind();
}
else
rptPages.Visible = false;
rptPosts.DataSource = pgitems;
rptPosts.DataBind();
}
protected void rptPosts_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Label lblSubject = e.Item.FindControl("lblSubject") as Label;
if (lblSubject.Text == string.Empty)
{
lblSubject.Text = "Re: " + Convert.ToString(Session["TopicName"]);
}
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
rptPages.ItemCommand +=
new RepeaterCommandEventHandler(rptPages_ItemCommand);
}
void rptPages_ItemCommand(object source,
RepeaterCommandEventArgs e)
{
PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
//fillPageData();
//fillReply();
LoadData(Convert.ToInt32(Request.QueryString["TopicId"]));
}
public int PageNumber
{
get
{
if (ViewState["PageNumber"] != null)
return Convert.ToInt32(ViewState["PageNumber"]);
else
return 0;
}
set
{
ViewState["PageNumber"] = value;
}
}
Will describe latter when i get some time but hope this helps you all
.aspx Content
------------------
<asp:Repeater ID="rptPosts" runat="server" OnItemDataBound="rptPosts_ItemDataBound"
OnItemCommand="rptPosts_ItemCommand">
<ItemTemplate>
<asp:Label ID="lblSubject" runat="server" Text='<%#Eval("Subject")%>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
<asp:Repeater ID="rptPages" runat="server">
<HeaderTemplate>
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
<b>Page:</b>
</td>
<td>
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="btnPage" CommandName="Page" CommandArgument="<%#
Container.DataItem %>" CssClass="f-anchor" runat="server"><%# Container.DataItem %>
</asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
</td> </tr> </table>
</FooterTemplate>
</asp:Repeater>
.cs Content
----------------
private void LoadData(int topicId)
{
lnkDelTopic.CommandArgument = Convert.ToString(topicId);
lnkEditTopic.CommandArgument = Convert.ToString(topicId);
PagedDataSource pgitems = new PagedDataSource();
pl_post objPost = new pl_post();
objPost.TopicId = topicId;
postList = bl_forum.GetPostListByTopicId(objPost);
pgitems.DataSource = postList;
pgitems.AllowPaging = true;
pgitems.PageSize = 5;
pgitems.CurrentPageIndex = PageNumber;
if (pgitems.PageCount > 1)
{
rptPages.Visible = true;
ArrayList pages = new ArrayList();
for (int i = 0; i < pgitems.PageCount; i++)
pages.Add((i + 1).ToString());
rptPages.DataSource = pages;
rptPages.DataBind();
}
else
rptPages.Visible = false;
rptPosts.DataSource = pgitems;
rptPosts.DataBind();
}
protected void rptPosts_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Label lblSubject = e.Item.FindControl("lblSubject") as Label;
if (lblSubject.Text == string.Empty)
{
lblSubject.Text = "Re: " + Convert.ToString(Session["TopicName"]);
}
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
rptPages.ItemCommand +=
new RepeaterCommandEventHandler(rptPages_ItemCommand);
}
void rptPages_ItemCommand(object source,
RepeaterCommandEventArgs e)
{
PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
//fillPageData();
//fillReply();
LoadData(Convert.ToInt32(Request.QueryString["TopicId"]));
}
public int PageNumber
{
get
{
if (ViewState["PageNumber"] != null)
return Convert.ToInt32(ViewState["PageNumber"]);
else
return 0;
}
set
{
ViewState["PageNumber"] = value;
}
}
Will describe latter when i get some time but hope this helps you all
No comments:
Post a Comment