Paging using repeater control

E

Eugene Anthony

The problem with my coding is that despite removing the records stored
in the array list, the rptPages repeater
control is still visible. The rptPages repeater control displayes the
navigation link (1,2,3 so on). The code can be found in
SubscriptionCart.aspx.cs.



Default.aspx
------------

<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">

<table border="1" width="239" bordercolor="#FF0000" style="position:
relative">
<tr>
<td width="235">
<table border="0" width="232">
<tr>
<td width="93"><asp:Label ID="Label1" runat="server"
Font-Size="Small" ForeColor="Black" Text="subscription"
Width="49px"></asp:Label></td>
<td width="123"><asp:Label ID="Label2" runat="server"
Font-Size="Small" ForeColor="Black" Text="Label"
Width="46px"></asp:Label></td>
</tr>
<tr>
<td width="93"><asp:HyperLink ID="HyperLink1" runat="server"
Font-Size="Small"
NavigateUrl="SubscriptionCart.aspx">checkout</asp:HyperLink></td>
<td width="123"></td>
</tr>
</table>
</td>
</tr>
</table>
<br /><br />
&nbsp;<asp:Button ID="Button1" runat="server"
OnClick="Button1_Click" Style="left: 0px;
position: relative; top: -27px" Text="Button" />
<asp:HyperLink ID="HyperLink3" runat="server" Style="left:
-129px; position: relative;
top: 78px">1</asp:HyperLink>
<asp:HyperLink ID="HyperLink2" runat="server"
NavigateUrl="SubscriptionCart.aspx?GroupID=1"
Style="left: -74px; position: relative; top:
23px">First</asp:HyperLink>
<asp:HyperLink ID="HyperLink4" runat="server"
NavigateUrl="SubscriptionCart.aspx?GroupID=2"
Style="left: -105px; position: relative; top:
55px">Two</asp:HyperLink>
</form>
</body>
</html>


Default.aspx.cs
---------------

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int numSubscription;
numSubscription = ((ArrayList)this.Session["Address"]).Count;
Label2.Text = numSubscription.ToString();

if (numSubscription == 0)
{
HyperLink1.Enabled = false;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("SubscriptionCart.aspx");
}
}


SubscriptionCart.aspx
---------------------

<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="SubscriptionCart.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">

<table border="1" width="239" bordercolor="#FF0000" style="position:
relative">
<tr>
<td width="235">
<table border="0" width="232">
<tr>
<td width="93"><asp:Label ID="Label3" runat="server"
Font-Size="Small" ForeColor="Black" Text="subscription"
Width="49px"></asp:Label></td>
<td width="123"><asp:Label ID="Label2" runat="server"
Font-Size="Small" ForeColor="Black" Text="Label"
Width="46px"></asp:Label></td>
</tr>
<tr>
<td width="93"><asp:HyperLink ID="HyperLink1" runat="server"
Font-Size="Small"
NavigateUrl="SubscriptionCart.aspx">checkout</asp:HyperLink></td>
<td width="123"></td>
</tr>
</table>
</td>
</tr>
</table>
<br /><br />

<asp:Repeater ID="rptItems" runat="server"
OnItemCommand="rptItems_ItemCommand">
<HeaderTemplate>
<table>
<tr>
<td bgcolor="#FFCC99">GroupID</td><td
bgcolor="#FFCC99">MemberID</td><td bgcolor="#FFCC99"></td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("GroupID") %></td><td><%# Eval("MemberID")
%></td><td><asp:LinkButton ID="LinkButton2"
runat="server">Remove</asp:LinkButton></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
&nbsp;
<br />
<asp:Repeater ID="rptPages" Runat="server">
<HeaderTemplate>
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td><b>Page:</b>&nbsp;</td>
<td>
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="btnPage"
CommandName="Page"
CommandArgument="<%#
Container.DataItem %>"
CssClass="text"
Runat="server"><%# Container.DataItem %>
</asp:LinkButton>&nbsp;
</ItemTemplate>
<FooterTemplate>
</td>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
<br /><br />

<table border="0" width="309">
<tr>
<td style="width: 144px; height: 21px;"><asp:HyperLink
ID="HyperLink4" runat="server" NavigateUrl="Default.aspx">return to
subscription</asp:HyperLink></td>
<td style="width: 166px; height: 21px;"><asp:LinkButton
ID="LinkButton1" runat="server"
OnClick="LinkButton1_Click">Confirm</asp:LinkButton></td>
</tr>
</table>

</form>
</body>
</html>




SubscriptionCart.aspx.cs
------------------------

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Data.SqlClient;
using MyProject;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
BindData();
UpdateSubCounter();
}

public int PageNumber
{
get
{
if (ViewState["PageNumber"] != null)
return Convert.ToInt32(ViewState["PageNumber"]);
else
return 0;
}
set
{
ViewState["PageNumber"] = value;
}
}

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
rptPages.ItemCommand +=
new RepeaterCommandEventHandler(rptPages_ItemCommand);
}

public void BindData()
{
String GroupID = Request.QueryString["GroupID"];

if (GroupID != null)
{
Subscription sub = new Subscription();
sub.groupid = int.Parse(GroupID.ToString()); //1;
sub.memberid = 2;

if (((ArrayList)this.Session["Address"]).Contains(sub) ==
false)
{
((ArrayList)this.Session["Address"]).Add(sub);
LoadData();
}
else
{
LoadData();
}
}
else
{
LoadData();
}
}

public void LoadData()
{
PagedDataSource pgitems = new PagedDataSource();
pgitems.DataSource = ((ArrayList)this.Session["Address"]);
pgitems.AllowPaging = true;
pgitems.PageSize = 1;
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 = true;
}

rptItems.DataSource = pgitems;
rptItems.DataBind();
}

public void UpdateSubCounter()
{
int numSubscription;
numSubscription = ((ArrayList)this.Session["Address"]).Count;
Label2.Text = numSubscription.ToString();

if (numSubscription == 0)
{
HyperLink1.Enabled = false;
LinkButton1.Enabled = false;
}
}

protected void LinkButton1_Click(object sender, EventArgs e)
{
for (int rowindex = 0; rowindex <
((ArrayList)this.Session["Address"]).Count; rowindex++)
{
Subscription m =
(Subscription)((ArrayList)this.Session["Address"])[rowindex];
SqlConnection cnn = new
SqlConnection(ConfigurationManager.ConnectionStrings["myConnection"].Con
nectionString);
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = cnn;
myCommand.CommandText = "INSERT subscription VALUES(" +
m.groupid + "," + m.memberid + ")";
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
myAdapter.Fill(ds, "subscription");
}

Response.Redirect("SubscriptionListing.aspx");
}
protected void rptItems_ItemCommand(object source,
RepeaterCommandEventArgs e)
{
LinkButton m = (LinkButton)e.Item.FindControl("LinkButton2");
int rowindex = m.TabIndex;

((ArrayList)this.Session["Address"]).Remove(((ArrayList)this.Session["Ad
dress"])[rowindex]);
LoadData();
UpdateSubCounter();
}

protected void rptPages_ItemCommand(object source,
RepeaterCommandEventArgs e)
{
PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
LoadData();
}
}


Subscription.cs
---------------


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for subscription
/// </summary>

namespace MyProject
{
public class Subscription
{
int _groupid, _memberid;

public int groupid
{
get { return _groupid; }
set { _groupid = value; }
}

public int memberid
{
get { return _memberid; }
set { _memberid = value; }
}

public override bool Equals(Object o)
{
Subscription s = (Subscription)o;
return (s.groupid == this.groupid && s.memberid ==
this.memberid);
}
}
}


How do I solve the problem?.


Your help is kindly appreciated.

Eugene Anthony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,698
Latest member
LydiaHalle

Latest Threads

Top