[thelist] .NET 2.0: Adding Multiple Controls

Peter Brunone (EasyListBox.com) peter at easylistbox.com
Wed Apr 11 22:01:57 CDT 2007


    Sounds like you're doing a postback each time, at which point the controls you created during the previous postback are not remembered.

    What if you were to keep a record of how many new rows you have, and loop to create that many plus one?

----------------------------------------

From: "Casey Crookston" caseyc at IntelliSoftmn.com

Ok, here's the (simple) concept:

A user will be manually entering rows of data. We don't know ahead of
time how many rows he'll be adding. Each row consists of a drop down, a
text field and a label. I have made a simple little user control which
contains these items. 

At the right time, the first instance of the control is added to a
placeholder. 

Sub AddLineItemRow()
ViewState("ItemCount") = ViewState("ItemCount") + 1
Dim ctlLineItems As Control =
LoadControl("admin_distribute_points_line_items.ascx")
ctlLineItems.ID = "ctlLineItems" & ViewState("ItemCount")
' populate drop down
plhLineItems.Controls.Add(ctlLineItems)
End Sub

Here's the catch. Outside of the control, I have a button with text
"Add New Row". When this button is clicked, we re-execute
AddLineItemRow(). This works, BUT, the previous row vanishes. In other
words, on the button click, a new Control is added with the ID of
ctlLineItems2, but ctlLineItems1 is gone. I need it to stay there.

What am I doing wrong. And, is there a better way to do this?

Thanks, Casey



More information about the thelist mailing list