简体   繁体   中英

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;
        }
    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM