Consecutive Additions of Controls

  • Thread starter Thread starter Matthew MacDonald
  • Start date Start date
M

Matthew MacDonald

Hi all,
I'm trying to make a web form that will allow the user to continuously add
options to a form by clicking a button.

Here is my sample code.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim drop As New DropDownList



tblMain.Rows(13).Cells(1).Controls.Add(drop)

End Sub

I want this to keep adding new drop down lists to the table whenever the
user clicks the button. What happens, however, is that the page reloads and
only one new control is ever added to the page (or more exactly, the
previous new control is wiped out and a new one is added, thus only ever
having one)


Any ideas on this??

Thanks, Matt
 
you have to re-add dynamic controls on postback, so you will need to
remember that you added them.

-- bruce (sqlwork.com)
 
Tables don't maintain their state in postback. You can track how often a
row has been added in viewstate and add that amount:

dim count as integer = 0
if not ViewState("RowCount") is nothing then
count = cint(viewstate("RowCount"))
end

count += 1
for i as iteger = 0 to count
'ADD row
next
viewstate("RowCount") = count

Karl
 
OK, that all makes sence. The problem however is that the controls
(specificaly dropdownlists) that have been added will probably have user
selections in them. When I add another one, I want the users selections for
the first controls to be maintained. I'm sure I could keep track of all of
this, but there has got to be a better way. Can you push a stack or que
onto the session or something like that?

Matt
 
Back
Top