tags:

views:

76

answers:

2

currently I'm writing code to make variable sized tables with asp:controls in them that look something like this:

TableRow tableHeader = new TableRow();
 tableHeader.ID = "tableHeader";
 userTable.Rows.Add(tableHeader);

  TableCell tableHeaderCell_1 = new TableCell();
  tableHeader.Cells.Add(tableHeaderCell_1);

  TableCell tableHeaderCell_2 = new TableCell();
   Label lblThc2 = new Label();
   lblThc2.Text = "<b>Name:</b>";
   tableHeaderCell_2.Controls.Add(lblThc2);
  tableHeader.Cells.Add(tableHeaderCell_2);

  TableCell tableHeaderCell_3 = new TableCell();
   Label lblThc3 = new Label();
   lblThc3.Text = "<b>User</b>";
   tableHeaderCell_3.Controls.Add(lblThc3);
  tableHeader.Cells.Add(tableHeaderCell_3);

  ...

and bodies that look like:

for(int i = 0; i < dynamicTableLength; i++)
{
    TableRow tableBodyTemp = new TableRow();
    tableBodyTemp.ID = "tableBody" + i;
 userTable.Rows.Add(tableBodyTemp);

  TableCell tableBodyCell_1 = new TableCell();
                        Label lblThc2 = new Label();
      lblThc2.Text = "<b>"+ i +"</b>";
      tableBodyCell_1.Controls.Add(lblThc2);
  tableHeader.Cells.Add(tableHeaderCell_1);

  TableCell tableBodyCell_2 = new TableCell();
   TextBox text = new TextBox();
                            text.ID = "name" + i;
                            text.GroupName = "myGroup";
  tableHeader.Cells.Add(tableHeaderCell_2);

                    ...

}

There has to be a better way to create tables/forms when you don't know how many rows you need.... or in general!

Can someone help?

+4  A: 

Use a repeater control. This lets you keep the markup within the aspx page. It will also allow you to change the markup very easily. You won't be restricted to a table - you can easily change it to divs or lists, if necessary at a later date.

darasd
A: 

Just create some helper methods to make your code more readable. You've got several copy/paste sections that can be refactored, and that should improve your situation a lot.

hova