How to add a new field each time a button is clicked

I want to create a new fileupload field each time i click on a button. i have a form in a page and i want to add the fields to that form. i tried to save an array of fileupload fields in a session but it deosnt seem to work.

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
        this.Session["fileUploadArray"] = new FileUpload[5];

}
protected void Button1_Click(object sender, EventArgs e)
{
    FileUpload[] flArray = ((FileUpload[])(this.Session["fileUploadArray"]));
    for (int i = 0; i < flArray.Length; i++)
    {
        if (flArray[i] == null)
        { 
            flArray[i] = new FileUpload();
            form1.Controls.Add(flArray[i]);
            this.Session["fileUploadArray"] = flArray;
            return;
        }
    }
}

Answers


Dynamically controls must be added in Page_Init event, refer to answer of mine on following post:

Dynamically generated buttons, on click not being executed


Need Your Help

File reading and writing C

c struct fopen scanf fwrite

I have a problem reading and writing to a file. I use fwrite and write a amount of struct arrays. After that I write to the file if there isn't one.

UIStoryboardSegue in MVC-model

ios xcode directory-structure

I am currently making up a directory structure, which is supposed to enable me to work clearer with the MVC model in my iOS-projects. I therefore created a folder for my Models, Views, Controllers ...