Hello everybody, please help me to create dynamically upload file. here is my code:
for (int counter = 1; counter <= 4; counter++)
{
FileUpload fu = new FileUpload();
fu = (FileUpload)FindControl("FileUpload" + counter.ToString());
this.Controls.Add(fu);
}
it return error on " this.Controls.Add(fu);" it said that Value cannot be null.
Parameter name: child
How to solve this? please help.
THanks
If there are no controls with names: "FileUpload1", "FileUpload2"...(etc)
Then when you use the FindControl() method, it will return null/nothing.
Thus, when you attempt to add the control, you are receiving this error.
What you need to do is declare And instantiate these variables earlier in your code. You should do this in your Page_Init method. The reason for this is because the Page_Init happens before your ViewState is loaded for each control on the page. This way you have declared and instantiated the necessary controls and then the Object's ViewState (containing the Object's properties) is set for you in the next ASP.NET step.
So you'll have something like:
-
Private myFileUploads(4) As FileUpload
-
-
Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
-
'This event happens before any controls are initialized by ASP.NET
-
'The ViewState for objects has not been loaded.
-
'After this event happens, the ViewState is loaded for each control and the object's properties are filled with the values submitted.
-
-
For x As Integer = 0 To myFileUploads.Length - 1
-
myFileUploads(x) = New FileUpload
-
-
Pnl_Uploads.Controls.Add(textBoxArr(x)) 'Adding the FileUploads to the Panel that holds the FileUploads.
-
Next
-
-
End Sub
-
Does this make sense?
-Frinny