creating dynamic control in asp.net

I’m creating a fileupload control on a linKbutton click event. First time it’s creating the controls, but if I press the link button second time, it’s not creating. What is the problem with that? The following is my code:

protected void LinkButton1_Click(object sender, EventArgs e)
{
    newattach();
}

private void newattach()
{
    int i;
    for (i = 0; i < 2; i++)
    {
        count++;
        FileUpload f1 = new FileUpload();
        f1.ID = "fileupload" + count.ToString();
        f1.Height = 34;
        f1.Width = 212;
        Panel1.Controls.Add(f1);
    }
}

and count is a static variable. Please help.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

When you create controls dynamically with ASP.NET you need to recreate the control every time you post back, generally you recreate the control on Page_Load. That is most likely the cause of your problem.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply