[thelist] .NET 2.0: Adding Multiple Controls

Casey Crookston caseyc at IntelliSoftmn.com
Wed Apr 11 16:04:55 CDT 2007


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