Jump to content
  • Advertisement
Sign in to follow this  
ChemicalImbalance

[web] ASP.NET - Creating tables dynamically

This topic is 4303 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 = new Label();
   lblItemName.Visible = true;
   lblItemName.Text = GetItemName(nID);
   form1.Controls.Add(lblItemName);

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!