Dynamic Textbox adding, and recreating them on postback

Posted on 2006-05-26
Last Modified: 2012-06-21
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.

Question by:macros14
    LVL 1

    Author Comment

    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.
    LVL 26

    Accepted Solution

    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 ..


    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    I have developed many web applications with asp & and to add and use a dropdownlist was always a very simple task, but with the new, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now