Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Dynamicly Add Controls

Reply
Thread Tools

Dynamicly Add Controls

 
 
gilly3
Guest
Posts: n/a
 
      07-24-2004
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.
 
Reply With Quote
 
 
 
 
Jos
Guest
Posts: n/a
 
      07-24-2004
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it possible to add a server-side form dynamicly? Owen Wong ASP .Net 0 09-12-2006 01:24 AM
dynamicly add link button to calendar cel =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?= ASP .Net 4 05-13-2005 08:57 PM
Add a TextObject Dynamicly Programmer ASP .Net 1 02-15-2005 04:41 PM
add event handler to butoon dynamicly =?Utf-8?B?bWFoc2E=?= ASP .Net 1 05-09-2004 05:06 PM
add button dynamicly to page mahsa ASP .Net Building Controls 1 05-09-2004 05:03 PM



Advertisments