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

x
?
Solved

Help with Dynamically creating controls

Posted on 2007-10-15
1
Medium Priority
?
149 Views
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();
           tblInvoice.Rows.Add(tRow);
           TableCell tCell = new TableCell();
           tRow.Cells.Add(tCell);
// add cell One content
            Label lblInvoice = new Label();
            lblInvoice.ID = "lblInvoice";
            lblInvoice.Text = "Invoice #:";
            tCell.Controls.Add(lblInvoice);
// add the cell to the Cells collection
            tRow.Cells.Add(tCell);
// add cell Two
            TableCell tCell2 = new TableCell();
            tRow.Cells.Add(tCell2);
// add cell Two content
            TextBox txtInvoice = new TextBox();
            txtInvoice.ID = "txtInvoice:";
            txtInvoice.Text = "";
            txtInvoice.Height = 18;
            tCell2.Controls.Add(txtInvoice);
// add the cell to the Cells collection
            tRow.Cells.Add(tCell2);
// -----AMOUNT-----
// add row Two
            TableRow tRow2 = new TableRow();
            tblInvoice.Rows.Add(tRow2);
// add cell Three
            TableCell tCell3 = new TableCell();
             tRow2.Cells.Add(tCell3);
// add cell Three content
             Label lblAmount = new Label();
             lblAmount.ID = "lblAmount";
             lblAmount.Text = "Amount:";
             tCell3.Controls.Add(lblAmount);
// add the cell to the Cells collection
              tRow2.Cells.Add(tCell3);
// add cell Four
              TableCell tCell4 = new TableCell();
              tRow2.Cells.Add(tCell4);
// add cell Four content
               TextBox txtAmount = new TextBox();
               txtAmount.ID = "txtAmount";
               txtAmount.Text = "";
               txtAmount.Height = 18;
               tCell4.Controls.Add(txtAmount);
// add the cell to the Cells collection
               tRow2.Cells.Add(tCell4);

// -----DATE-----
// add row Three
            TableRow tRow3 = new TableRow();
            tblInvoice.Rows.Add(tRow3);
// add cell Five
             TableCell tCell5 = new TableCell();
             tRow3.Cells.Add(tCell5);
// add cell Five content
             Label lblDateRecieved = new Label();
             lblDateRecieved.ID = "lblDateRecieved";
             lblDateRecieved.Text = "Date:";
             tCell5.Controls.Add(lblDateRecieved);
// add the cell to the Cells collection
             tRow3.Cells.Add(tCell5);
// add cell Six
             TableCell tCell6 = new TableCell();
             tRow3.Cells.Add(tCell6);
// 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;
             tCell6.Controls.Add(txtDateRecieved);
             tCell6.Controls.Add(btnCalendar);
// add the cell to the Cells collection
              tRow3.Cells.Add(tCell6);

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

Accepted Solution

by:
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

or

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
0

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