Help with Dynamically creating controls

Posted on 2007-10-15
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

    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

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    This video discusses moving either the default database or any database to a new volume.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now