If I dynamically add controls in ASP.NET, how do I retrieve their values on subsequent posting?

Posted on 2007-08-08
Last Modified: 2012-05-05
If I dynamically add controls in ASP.NET, how do I retrieve their values on subsequent posting?
For example this form:

<form id="form1" runat="server">
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList><br />
        <asp:Panel ID="Panel1" runat="server" Height="50px" Width="436px">
            <asp:FileUpload ID="fileUpload1" runat="server" Width="429px" /></asp:Panel>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        <br />
        <br />
        <asp:ListBox ID="ListBox1" runat="server" Width="379px"></asp:ListBox></div>

has a dropdown list that when changed runs this:

        for (int i = 2; i < Convert.ToInt32(DropDownList1.SelectedIndex)+ 2; i++)
            FileUpload fileUpload = new FileUpload();
            fileUpload.ID = "fileUpload" + i.ToString();

I need code for the button that will get the file names from however many controls were added with the drop down list.
Question by:ksaul
    LVL 96

    Assisted Solution

    by:Bob Learned
    If you dynamically add controls, then you need to recreate them in the Page_Init event handler.

    LVL 69

    Accepted Solution

    LVL 10

    Author Comment

    Can you give me some sample code of how to do that?  I would need to retain entered values when I re-create them.
    LVL 96

    Assisted Solution

    by:Bob Learned
    If the page has the ViewState enabled, and the control has a ViewState, then when you recreate the control in the Page_Init, the entered values will be retained.  The problem that you are going to have is that the FileUpload control will not retain it's previous value, nor can you specify one.

    LVL 10

    Author Comment

    I'm going to split points for your suggestions.

    I decided that for this situation it was better to create 25 design-time FileUpload controls setting their style property to display:none and then use javascript to show and enable as many as the user needed.  That made looping through the controls after the post much easier.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    761 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

    7 Experts available now in Live!

    Get 1:1 Help Now