Sign in to follow this  
ChemicalImbalance

[web] ASP.NET - Creating tables dynamically

Recommended Posts

In ASP.net how would I go about creating a table dynamically? I'm trying to create an online catalog. I've succesfully added other controls dynamically:
   lblItemName[i] = new Label();
   lblItemName[i].Visible = true;
   lblItemName[i].Text = GetItemName(nID);
   form1.Controls.Add(lblItemName[i]);

But I haven't been able to figure out how to do this with a table. Help in C#.net or VB.net would be great.

Share this post


Link to post
Share on other sites
Normally I'd suggest using a asp:Repeater and databinding it to the values you want in the table (either an ADO.NET thing like a DataTable object, or if you're just inventing the values, an ArrayList or something).

Mark

Share this post


Link to post
Share on other sites
Try something like this:


Table tbl = new Table();
form1.Controls.Add(tbl);

TableRow tr = new TableRow();
tbl.Rows.Add(tr);

TableCell td = new TableCell();
tr.Cells.Add(td);

td.Text = "Hello";

td = new TableCell();
tr.Cells.Add(td);

td.Text = "World!";

Share this post


Link to post
Share on other sites
If you do the above, make sure you add the table to the forms child collection at the end. If you add it in the beginning you bind your table early to the control - which doesn't really matter unless you want to change chrome stuff at some point later. If you add it at the end there's less things triggered by each change to the control while you are adding children to it.

Keep in mind that there are built in web controls that do all kinds of nice things for you as far as data formatting etc goes. Investigate these for your online catalog first to make sure you aren't reinventing a wheel.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this