plazza sele

plazza sele

  • NA
  • 15
  • 21.3k

Adding dynamically created table to panel

May 9 2011 6:08 AM
Hi everyone.
I am making three search methods, i have three text bokses and three bottuns. search firstname, lastname, birthday. and a im retriving a list of
an object List<person> for each search. they are seperate search methods.
i am then creating Table table = new Table(); running for( int i = 0; i < list.count ; i++). and adding new row on each loop and adding a link button
to each row to view more of the person object. the id of the linkbuttons is dynamically ( i ) .
my problem is that when i have only one search option the linkbuttons works fine, but when i make the to other searches, i cant get the view more linkbutton event. can any body help me with this ??
I am using N-Tier arcitecture.i am writing my code here, assume that there is an object Person.
this is my code:
file.aspx
<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" />
    <br />
       Lastname:
       <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
       <asp:Button ID="Button2" runat="server" Text="search" />
    <br />
       Birthday:
       <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
       <asp:Button ID="Button3" runat="server" Text="search" />
   </asp:Panel>
   <asp:Panel ID="PanelResult" runat="server">
   </asp:Panel>
   <asp:Panel ID="PanelViewmore" runat="server">
   </asp:Panel>
</form>
</body>

file.aspx.cs
private List<Person> listing;
protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack)
   {
      PanelContainer.Visible = true;
      PanelResult.Visible = false;
      PanelViewmore.Visible = false;
      firstnameTable();
      lastnameTable();
   }
}
// Firstname search
protected void Button1_Click(object sender, EventArgs e)
{
    PanelResult.Controls.Clear();
    firstnameTable();
}
public void firstnameTable()
{
    Label lblResult = new Label();
    PanelResult.Visible = true;
    PanelViewmore.Visible = false;
    string firstname = TextBox1.Text;
    var control = new PersonControl();
    List<Person> retur = control.getAlleFirstname(firstname);
    listing = null;
    listing = retur;
    if ( retur.Count() == 0 || retur == null) 
    {
       lblResult.Text = "<br />No result.";
       PanelResult.Controls.Add(lblResult);  
    }
    else
    {
       int rowNumber = retur.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 = retur[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)
{
       PanelResult.Controls.Clear();
       PanelViewmore.Controls.Clear();
       lastnameTable();
}
public void lastnameTable()
{
       PanelResult.Controls.Clear();
       Label lblResult = new Label();
       PanelResult.Visible = true;
       PanelViewmore.Visible = false;
       string lastname = TextBox2.Text;
       var control = new PersonControl();
       List<Person> retur = control.getAlleLastnamde(lastname);
       listing = null;
       listing = retur;
       if (retur.Count() == 0 || retur == null)
       {
             lblResult.Text = "<br />No result.";
             PanelResult.Controls.Add(lblResult);
       }
       else
       {
             int rowNumber = retur.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 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(viewMore_Click);

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

                  Person t = retur[i];
                  TableCell lName = new TableCell();
                  TableCell fName = 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(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);
    }
}
// birthday search
protected void Button3_Click(object sender, EventArgs e)
{

}

protected void viewMore_Click(object sender, EventArgs e)
{
     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 = listing[listeNumber];
     int x = catchTheObject.regNr;

     Label a = new Label();
      a.Text = "<br />This is the viewMore panel and the object number is " + x;
     PanelViewmore.Controls.Add(a);
  }


Answers (2)