Dynamicly Add Controls

Discussion in 'ASP .Net Web Controls' started by gilly3, Jul 24, 2004.

  1. gilly3

    gilly3 Guest

    I need to be able to add form inputs to a form. A user can enter one item
    and then click a link to add another.

    So far I have a linkbutton the calls this function:

    private void linkAddRow_Click(object sender, System.EventArgs e)
    {
    HtmlTableRow oNewRow = new HtmlTableRow();
    HtmlTableCell oNewCell = new HtmlTableCell();
    TextBox oNewTextBox = new TextBox();
    oNewCell.Controls.Add(oNewTextBox);
    oNewRow.Cells.Add(oNewCell);
    oTable.Rows.Add(oNewRow);
    }

    It adds one row fine. But each additional time I click the link, I don't
    get any more rows. Instead of generating a new row, it is only modifying
    the existing object.

    What approach should I use to dynamically add an unknown number of elements
    to a page?

    -ivan.
     
    gilly3, Jul 24, 2004
    #1
    1. Advertising

  2. gilly3

    Jos Guest

    gilly3 wrote:
    > I need to be able to add form inputs to a form. A user can enter one
    > item and then click a link to add another.
    >
    > So far I have a linkbutton the calls this function:
    >
    > private void linkAddRow_Click(object sender, System.EventArgs e)
    > {
    > HtmlTableRow oNewRow = new HtmlTableRow();
    > HtmlTableCell oNewCell = new HtmlTableCell();
    > TextBox oNewTextBox = new TextBox();
    > oNewCell.Controls.Add(oNewTextBox);
    > oNewRow.Cells.Add(oNewCell);
    > oTable.Rows.Add(oNewRow);
    > }
    >
    > It adds one row fine. But each additional time I click the link, I
    > don't get any more rows. Instead of generating a new row, it is only
    > modifying the existing object.
    >
    > What approach should I use to dynamically add an unknown number of
    > elements to a page?
    >
    > -ivan.


    Dynamic controls don't get recreated automatically on postback.
    Therefore, you should make sure they are recreated in your code.

    I suggest adding a session or viewstate variable to keep track
    of how many rows the user has, and using that variable
    to recreate the table on postback.

    --

    Jos
     
    Jos, Jul 24, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?bWFoc2E=?=

    add event handler to butoon dynamicly

    =?Utf-8?B?bWFoc2E=?=, May 9, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    490
    Victor Garcia Aprea [MVP]
    May 9, 2004
  2. Programmer

    Add a TextObject Dynamicly

    Programmer, Feb 15, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    413
    Chris Tacke, eMVP
    Feb 15, 2005
  3. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    dynamicly add link button to calendar cel

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, May 11, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    6,342
    Karl Seguin
    May 13, 2005
  4. Owen Wong
    Replies:
    0
    Views:
    340
    Owen Wong
    Sep 12, 2006
  5. mahsa

    add button dynamicly to page

    mahsa, May 9, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    128
    Victor Garcia Aprea [MVP]
    May 9, 2004
Loading...

Share This Page