Link to home
Start Free TrialLog in
Avatar of ksaul
ksaul

asked on

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

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">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem>1</asp:ListItem>
            <asp:ListItem>2</asp:ListItem>
            <asp:ListItem>3</asp:ListItem>
        </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>
    </form>

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();
            Panel1.Controls.Add(fileUpload);
        }

I need code for the button that will get the file names from however many controls were added with the drop down list.
SOLUTION
Avatar of Bob Learned
Bob Learned
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
ASKER CERTIFIED SOLUTION
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
Avatar of ksaul
ksaul

ASKER

Bob,
Can you give me some sample code of how to do that?  I would need to retain entered values when I re-create them.
SOLUTION
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
Avatar of ksaul

ASKER

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.