Dynamic Textbox adding, and recreating them on postback

I have a custom user control that I have built called Rooms.ascx.  It contains 2 textboxes, room_name and room_description.  

On the page that I want to use the control I have the following code.
<asp:Panel id="pnlRoomCollection" Runat="server">
          <MHL:Rooms id="room_1" runat="server"></MHL:Rooms>
<asp:Button id="cmdAddRoom" runat="server" Text="Add Another Room"></asp:Button>

Now when I click cmdAddRoom, I want to add another user control to the panel.  
Private Sub cmdAddRoom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddRoom.Click
        Dim o As Control = LoadControl("Rooms.ascx")
    End Sub

This works the first time I click the button, but after I click the button the second time, the user control is created but overrides my previous control.  How do I recreate the controls in the panel on postback?  I am guess on page_init but I can't seem to find my controls that were on the page before postback in the panel.

Who is Participating?
RejojohnyConnect With a Mentor Commented:
good that u solved it .. but i would advice u against using session variables as the scope of the "room objects" values is needed only for the current page .. but storing in the session actually makes it scope for all the pages in the session which is not needed ... so it is avlive when u no longer need it too .. u could use the "viewstate" for this current requirement .. the syntax is similar to one u use for session ..

just replace session with viewstate
viewstate("RoomObjects") = UrValue
urVariable = viewstate("RoomObjects")

the advantage is that when the user navigates to another page the viewstate is no longer is scope and so u save valuable memory resource of ur web server .. and as u know viewstate is maintained by the page ..

macros14Author Commented:
I figured it out, what I had to do was store how many room objects there were in session, and then readd them with the correct id on page init.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.