Link to home
Start Free TrialLog in
Avatar of macros14
macros14

asked on

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:Panel>
<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")
        pnlRoomCollection.Controls.Add(o)
    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.


Avatar of macros14
macros14

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Rejojohny
Rejojohny
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial