Paging dynamically created table
Hi everyone...
I am creating this table dynamically and was wondering how to create some paging when a table is getting long.
I have been searching with no result. could any body please see the code and try it. the code is ready to run.
Default.aspx
<body>
<form id="form1" runat="server">
<div>
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">View list</asp:LinkButton>
</div>
<br />
<br />
<asp:Panel ID="PanelResult" runat="server">
</asp:Panel>
<asp:Panel ID="PanelViewmore" runat="server">
</asp:Panel>
</form>
</body>
Default.aspx.cs
public class Person
{
public string fname;
public string lname;
public string birthdate;
public int pnumber;
}
public partial class _Default : System.Web.UI.Page
{
private static List<Person> personList = new List<Person>();
private static List<Person> returFirstname = new List<Person>();
private static List<Person> returLastname = new List<Person>();
static bool firstPageRun = new bool();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
firstPageRun = true;
PanelResult.Visible = false;
PanelViewmore.Visible = false;
createPersonList();
}
viewList();
}
void createPersonList()
{
personList.Clear();
Person a = new Person(); a.fname = "Tom"; a.lname = "Hansen"; a.birthdate = "12.07.1981"; a.pnumber = 2;
Person b = new Person(); b.fname = "Ole"; b.lname = "Nordman"; b.birthdate = "12.07.1983"; b.pnumber = 8;
Person c = new Person(); c.fname = "Kim"; c.lname = "Haderson"; c.birthdate = "10.07.1975"; c.pnumber = 15;
Person d = new Person(); d.fname = "Tom"; d.lname = "Petterson"; d.birthdate = "13.07.1980"; d.pnumber = 4;
Person e = new Person(); e.fname = "Tom"; e.lname = "Hansen"; e.birthdate = "12.06.1981"; e.pnumber = 10;
Person f = new Person(); f.fname = "Ole"; f.lname = "Carlsen"; f.birthdate = "03.06.1981"; f.pnumber = 16;
Person g = new Person(); g.fname = "Tom"; g.lname = "Pettersen"; g.birthdate = "13.06.1980"; g.pnumber = 11;
Person h = new Person(); h.fname = "Tom"; h.lname = "Møller"; h.birthdate = "03.06.1981"; h.pnumber = 13;
Person i = new Person(); i.fname = "Tom"; i.lname = "Hansen"; i.birthdate = "03.05.1981"; i.pnumber = 14;
Person j = new Person(); j.fname = "Ulf"; j.lname = "Hansen"; j.birthdate = "03.06.1981"; j.pnumber = 18;
Person k = new Person(); k.fname = "Tom"; k.lname = "Pettersen"; k.birthdate = "03.06.1981"; k.pnumber = 22;
Person l = new Person(); l.fname = "Tom"; l.lname = "Petterson"; l.birthdate = "03.06.1982"; l.pnumber = 20;
Person m = new Person(); m.fname = "Tom"; m.lname = "Hansen"; m.birthdate = "03.06.1981"; m.pnumber = 25;
personList.Add(a); personList.Add(b); personList.Add(c); personList.Add(d); personList.Add(e);
personList.Add(f); personList.Add(g); personList.Add(h); personList.Add(i); personList.Add(j);
personList.Add(k); personList.Add(l); personList.Add(m);
}
void viewList()
{
if (firstPageRun != true)
{
Label lblResult = new Label();
lblResult.Text = "This is the hole list<br />";
int rowNumber = personList.Count();
Table table = new Table();
table.CellPadding = 8;
table.Width = 700;
TableHeaderRow thr = new TableHeaderRow();
TableHeaderCell thFirstname = new TableHeaderCell();
TableHeaderCell thLastname = new TableHeaderCell();
TableHeaderCell thDate = new TableHeaderCell();
TableHeaderCell thPNumber = new TableHeaderCell();
TableHeaderCell thMore = new TableHeaderCell();
thFirstname.HorizontalAlign = HorizontalAlign.Left;
thLastname.HorizontalAlign = HorizontalAlign.Left;
thDate.HorizontalAlign = HorizontalAlign.Left;
thPNumber.HorizontalAlign = HorizontalAlign.Left;
thMore.HorizontalAlign = HorizontalAlign.Left;
thFirstname.Text = "Firstname";
thr.Cells.Add(thFirstname);
thLastname.Text = "Lastname";
thr.Cells.Add(thLastname);
thDate.Text = "Birthday";
thr.Cells.Add(thDate);
thPNumber.Text = "Personal nr";
thr.Cells.Add(thPNumber);
thMore.Text = "View more";
thr.Cells.Add(thMore);
table.Rows.Add(thr);
for (int i = 0; i < rowNumber; i++)
{
TableRow tr = new TableRow();
LinkButton showmore = new LinkButton();
showmore.ID = i.ToString();
showmore.Text = "View more";
showmore.CommandArgument = i.ToString();
showmore.Click += new EventHandler(viewMore_Click);
TableCell tcmore = new TableCell();
tcmore.Controls.Add(showmore);
Person t = personList[i];
TableCell fName = new TableCell();
TableCell lName = new TableCell();
TableCell bDate = new TableCell();
TableCell pNumber = new TableCell();
fName.Text = t.fname;
lName.Text = t.lname;
bDate.Text = t.birthdate;
pNumber.Text = Convert.ToString(t.pnumber);
tr.Cells.Add(fName);
tr.Cells.Add(lName);
tr.Cells.Add(bDate);
tr.Cells.Add(pNumber);
tr.Cells.Add(tcmore);
table.Rows.Add(tr);
}
PanelResult.Controls.Add(lblResult);
PanelResult.Controls.Add(table);
PanelResult.Visible = true;
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
firstPageRun = false;
viewList();
}
protected void viewMore_Click(object sender, EventArgs e)
{
List<Person> result = personList;//(List<Person>)Session["firstname"];
PanelViewmore.Controls.Clear();
PanelResult.Visible = false;
PanelViewmore.Visible = true;
LinkButton b = sender as LinkButton;
int listeNumber = Convert.ToInt32(b.CommandArgument);
Person catchTheObject = result[listeNumber];
int x = catchTheObject.pnumber;
Label a = new Label();
a.Text = "<br />This is the viewMore panel and the object number is " + x;
PanelViewmore.Controls.Add(a);
}
}