Dynamic Controls Lost On Postback

Discussion in 'ASP .Net' started by vcuankitdotnet, Apr 16, 2007.

  1. Hi. I have tried to search for this online, but can't find what I'm
    looking for so I decided to come here. I have a ASP.NET 2.0 website
    with a masterpage. My .aspx page uses the masterpage. Here is the HTML
    from my .aspx page:

    <asp:Content ID="Content" ContentPlaceHolderID="MyContentArea"
    <asp:placeHolder ID="phControlHolder" runat="server"></
    asp:placeHolder><br /><br />
    <asp:Button ID="btnSubmitForm" runat="server" Text="Sign Up!"
    OnClick="btnSubmitForm_Click" />

    In my Page_Init method of my .aspx.cs page, I dynamically create a
    DropDownList web control and place it inside the phControlHolder
    control using:

    if (!Page.IsPostback)
    DropDownList ddl = new DropDownList();
    //add items inside the ddl

    When the user clicks the btnSubmitForm button, I would like to
    retrieve the dropdown that was dynamically created and store the value
    the user has picked. I have tried looping through all the controls on
    the page, and also looping through all of the controls inside the
    PlaceHolderControl, but I haven't been able to retrieve the
    dynamically created control.

    I feel as though I am losing the control once the page has posted
    back, but I don't really know how to address the situation. Any help
    would be greatly appreciated! Thanks!
    vcuankitdotnet, Apr 16, 2007
    1. Advertisements

  2. vcuankitdotnet

    Guest Guest

    The DDL 'code' you have written in "!Page.IsPostBack....therefore it will be
    available for first time only..once the page is reloaded or PostBack...there
    wil be noDDL control ..put it under just Page_Load event....

    Guest, Apr 16, 2007
    1. Advertisements

  3. When you create a dynamic control, you must accept the responsibility of
    re-creating the control upon each postback.
    I suggest you create the control in the Page Init event for every page
    request whether it's a postback or not.
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider], Apr 16, 2007
  4. But if I recreate the control when the page posts back, then won't I
    lose the values that the user has selected? When the user clicks on
    the button, I want to loop through all the dynamic controls created
    and retrieve the values that the user has selected/changed. Recreating
    the web controls will simply wipe out the values that were selected,
    right? Perhaps I am missing something else?
    vcuankitdotnet, Apr 16, 2007
  5. If you create your controls in the Page_Init event then following that the
    user entered values should automatically be filled in upon each postback so
    you an acces them.
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider], Apr 16, 2007
  6. Dynamically created controls must be RE-created upon EVERY "postback",
    "callback" or whateve...
    This is Gaia Ajax Widgets example, but it's exactly the same in
    "normal" ASP.NET:

    Click the "Show Code" button...!!
    Thomas Hansen, Apr 18, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.