I'm adding at my ASPX
page some controls dynamically. I need to add dynamically them, because the amount of controls depends on database record.
I'm using UpdatePanel
and adding controls dynamically like that:
void AddFileUploadFields()
{
for (int i = 0; i < Helper.uploadFieldsCount; i++)
{
FileUpload fileUpload = new FileUpload();
string controlId = DateTime.Now.Ticks.ToString();
Thread.Sleep(10); // for non-equal ID for each control
fileUpload.ID = controlId;
uploadFormsId.Add(controlId);
UpdatePanel1.ContentTemplateContainer.Controls.Add(fileUpload);
PlaceHolder1.Controls.Add(fileUpload);
}
}
Then from the button, which was defined statically in asp-tag <asp:Button ... />
I'm trying to handle the file info, which I want to upload:
void buttonUpload_Click(object sender, EventArgs e)
{
var iEnum = uploadFormsId.GetEnumerator();
List<UploadFormDetails> uploadsInfo = new List<UploadFormDetails>();
List<string> generatedFileNames = new List<string>();
bool wereErrors = false;
while (iEnum.MoveNext())
{
FileUpload uploadForm = (FileUpload)FindControl(iEnum.Current.ToString());
uploadsInfo.Add(new UploadFormDetails(uploadForm.ID, uploadForm.HasFile, uploadForm.FileName));
}
...
}
The main problem does occur here:
FileUpload uploadForm = (FileUpload)FindControl(iEnum.Current.ToString());
When I'm getting control like that, all options of it, like .HasFile
or .FileName
are unavailable to see. It just looks like a new control with no saved options. Of course, I'm selecting some file for the test, please don't think that I'm trying to upload from empty form.
How could i fix it? I can't upload any file...