plazza sele

plazza sele

  • NA
  • 15
  • 21.3k

Answer for the adding dynamically table and eventhandler for the linkbuttons

May 10 2011 6:47 PM
Hi everyone..

i had this topic http://www.c-sharpcorner.com/Forums/Thread/121756/adding-dynamically-created-table-to-panel.aspx for a couple of days ago. Now i have solved my problem and i startet a new post cause people checks the new posts. i hope this will be a god post and will be useful for who need it. and i have the code here it is ready to run if any body want's to try it and please if you have a better solution share it with os.

file.aspx

<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Panel ID="PanelContainer" runat="server">
Firstname:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="search" onclick="Button1_Click" />
<br />
Lastname:
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="search" onclick="Button2_Click" />
</asp:Panel>
<asp:Panel ID="PanelResult" runat="server">
</asp:Panel>
<asp:Panel ID="PanelViewmore" runat="server">
</asp:Panel>
</form>
</body>


file.aspx.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public class Person
{
public string fname;
public string lname;
public string birthdate;
public int pnumber;
}

public partial class file : 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;
PanelContainer.Visible = true;
PanelResult.Visible = false;
PanelViewmore.Visible = false;
createPersonList();
}
firstnameTable();
lastnameTable();
if (firstPageRun == true)
{
PanelResult.Visible = false;
PanelViewmore.Visible = false;
}
}

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 = "03.06.1981"; e.pnumber = 10;

personList.Add(a);
personList.Add(b);
personList.Add(c);
personList.Add(d);
personList.Add(e);

}
// Firstname search
protected void Button1_Click(object sender, EventArgs e)
{
TextBox2.Text="";
Session["firstname"] = null;
PanelResult.Controls.Clear();
PanelViewmore.Controls.Clear();
returLastname.Clear();
firstPageRun = false;
firstnameTable();
if (firstPageRun == true)
{
PanelResult.Visible=false;
PanelViewmore.Visible = false;
}
Session["firstname"] = returFirstname;

int x = 0;
}
public void firstnameTable()
{
Label lblResult = new Label();
PanelResult.Visible = true;
PanelViewmore.Visible = false;
string firstname = TextBox1.Text;
returFirstname.Clear();
for (int i = 0; i < personList.Count;i++)
{
if (firstname == personList[i].fname)
{
returFirstname.Add(personList[i]);
}
}
if (returFirstname.Count() == 0 && (firstPageRun == false))
{
lblResult.Text = "<br />No result.";
PanelResult.Controls.Add(lblResult);
}
else
{
int rowNumber = returFirstname.Count();
string message = "<strong>Your search have: </strong> " + rowNumber + " <strong>result</strong><br /><br />";
lblResult.Text = message;

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 = returFirstname[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);
}
}

// lastname search
protected void Button2_Click(object sender, EventArgs e)
{
TextBox1.Text = "";
Session["lastname"]=null;
PanelResult.Controls.Clear();
PanelViewmore.Controls.Clear();
returFirstname.Clear();
firstPageRun = false;
lastnameTable();
if (firstPageRun == true)
{
PanelResult.Visible = false;
PanelViewmore.Visible = false;
}
Session["lastname"] = returLastname;
}
public void lastnameTable()
{
Label lblResult = new Label();
PanelResult.Visible = true;
PanelViewmore.Visible = false;
string lastname = TextBox2.Text;
returLastname.Clear();
for (int i = 0; i < personList.Count; i++)
{
if (lastname == personList[i].lname)
{
returLastname.Add(personList[i]);
}
}
if (returLastname.Count() == 0 && (firstPageRun == false))
{
lblResult.Text = "<br />No result.";
PanelResult.Controls.Add(lblResult);
}
else
{
int rowNumber = returLastname.Count();
string message = "<strong><br />Your search for lastname have: </strong> " + rowNumber + " <strong>result</strong><br /><br />";
lblResult.Text = message;

Table table = new Table();
table.CellPadding = 8;
table.Width = 700;

TableHeaderRow thr = new TableHeaderRow();
TableHeaderCell thLastname = new TableHeaderCell();
TableHeaderCell thFirstname = 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;

thLastname.Text = "Lastname";
thr.Cells.Add(thLastname);
thFirstname.Text = "Firstname";
thr.Cells.Add(thFirstname);
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(viewMore2_Click);

TableCell tcmore = new TableCell();
tcmore.Controls.Add(showmore);

Person t = returLastname[i];
TableCell lName = new TableCell();
TableCell fName = new TableCell();
TableCell bDate = new TableCell();
TableCell pNumber = new TableCell();

lName.Text = t.lname;
fName.Text = t.fname;
bDate.Text = t.birthdate;
pNumber.Text = Convert.ToString(t.pnumber);

tr.Cells.Add(lName);
tr.Cells.Add(fName);
tr.Cells.Add(bDate);
tr.Cells.Add(pNumber);
tr.Cells.Add(tcmore);
table.Rows.Add(tr);
}
PanelResult.Controls.Add(lblResult);
PanelResult.Controls.Add(table);
}
}

protected void viewMore_Click(object sender, EventArgs e)
{
List<Person> result = returFirstname;//(List<Person>)Session["firstname"];
PanelViewmore.Controls.Clear();
PanelContainer.Visible = false;
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);
}
protected void viewMore2_Click(object sender, EventArgs e)
{
List<Person> result = returLastname; //(List<Person>)Session["lastname"];
PanelViewmore.Controls.Clear();
PanelContainer.Visible = false;
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);
}
}








Answers (1)