Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Help with Dynamically creating controls

Posted on 2007-10-15
Medium Priority
Last Modified: 2012-05-05
This is for the web using C#.

What i'm trying to do:
I would like everytime the user clicks a button a table with 3 rows and 2 columns are created. And 3 labels and 3 textboxes are created inside the table. The user can click on the button once, enter the info, then save the document. Reopen the document later and click the button again to add even more information. So if the user does this 4 times, there will be four tables with all of the information they entered.

I'm able to do this only once. When the i click the button it creates everything, but when i click it again it wipes it away and creates the same table over it..

I'll be working on this in the meantime, but any help with be greatly apprecaited.

I used the table control in the toolbox.
I tried:
Table tblInvoice = new Table() ;
but i get an error: "_Default.Table' does not contain a definition for 'Rows' ".
I'd like to create the table dynamically rather then dropping the control on the aspx page

here is my code.  

// -----INVOICE-----
           TableRow tRow = new TableRow();
           TableCell tCell = new TableCell();
// add cell One content
            Label lblInvoice = new Label();
            lblInvoice.ID = "lblInvoice";
            lblInvoice.Text = "Invoice #:";
// add the cell to the Cells collection
// add cell Two
            TableCell tCell2 = new TableCell();
// add cell Two content
            TextBox txtInvoice = new TextBox();
            txtInvoice.ID = "txtInvoice:";
            txtInvoice.Text = "";
            txtInvoice.Height = 18;
// add the cell to the Cells collection
// -----AMOUNT-----
// add row Two
            TableRow tRow2 = new TableRow();
// add cell Three
            TableCell tCell3 = new TableCell();
// add cell Three content
             Label lblAmount = new Label();
             lblAmount.ID = "lblAmount";
             lblAmount.Text = "Amount:";
// add the cell to the Cells collection
// add cell Four
              TableCell tCell4 = new TableCell();
// add cell Four content
               TextBox txtAmount = new TextBox();
               txtAmount.ID = "txtAmount";
               txtAmount.Text = "";
               txtAmount.Height = 18;
// add the cell to the Cells collection

// -----DATE-----
// add row Three
            TableRow tRow3 = new TableRow();
// add cell Five
             TableCell tCell5 = new TableCell();
// add cell Five content
             Label lblDateRecieved = new Label();
             lblDateRecieved.ID = "lblDateRecieved";
             lblDateRecieved.Text = "Date:";
// add the cell to the Cells collection
// add cell Six
             TableCell tCell6 = new TableCell();
// add cell Six content
             TextBox txtDateRecieved = new TextBox();
             txtDateRecieved.ID = "txtDateRecieved";
             txtDateRecieved.Text = "";
             txtDateRecieved.Height = 18;
             Button btnCalendar = new Button();
             btnCalendar.BackColor = System.Drawing.Color.White;
             btnCalendar.CssClass = "calendarImage";
             btnCalendar.Width = 27;
// add the cell to the Cells collection

Thanks a lot in advance.
Question by:JordanBlackler
1 Comment
LVL 14

Accepted Solution

steveberzins earned 1500 total points
ID: 20082747
do you really need to add multiple tables?

if it were me, I'd add rows to one table, that would seem to make this a bit simpler.

If you really need to add multiple tables, but are just using one table variable, like your example 'tblInvoice', you will need to create that table, add it to the .Controls collection of whatever you're putting the tables into, and then create another one, and do all this code over again, you'd have to do this similar thing creating table rows to put into one table, but it is a little more simple than adding than one more layer on top which you must do when creating multiple tables.

so, either,

create a table
create the row
create the columns and controls
add the table to .Controls of its container


create a row
create the columns and controls
add the row to the table

second way seems just a tad more simple.

but, if you create something, and don't add it to something before creating another one, you will only end up with the last one created, which I think is what you're seeing happen

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question