create dynamically uploadfile

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lyne_asp
    New Member
    • Aug 2006
    • 20

    create dynamically uploadfile

    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)Fin dControl("FileU pload" + counter.ToStrin g());
    this.Controls.A dd(fu);
    }

    it return error on " this.Controls.A dd(fu);" it said that Value cannot be null.
    Parameter name: child

    How to solve this? please help.

    THanks
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by lyne_asp
    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)Fin dControl("FileU pload" + counter.ToStrin g());
    this.Controls.A dd(fu);
    }

    it return error on " this.Controls.A dd(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: "FileUpload 1", "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:


    [code=vbnet]
    Private myFileUploads(4 ) As FileUpload

    Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArg s) 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.L ength - 1
    myFileUploads(x ) = New FileUpload

    Pnl_Uploads.Con trols.Add(textB oxArr(x)) 'Adding the FileUploads to the Panel that holds the FileUploads.
    Next

    End Sub
    [/code]

    Does this make sense?


    -Frinny

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      As an addition, if you don't supply a unique ID to the controls in the loop, you will not be able to find them with FindControl() (well maybe you can, but they take on some default unique names)

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by Plater
        As an addition, if you don't supply a unique ID to the controls inthe loop, you will not be able to find them with FindControl() (well maybe you can, but they take on some default unique names)
        I don't think you even need to use the FindControl().
        You can just use: myFileUploads[theIndexOfTheCo ntrol] to refer to the one you want...

        Comment

        • lyne_asp
          New Member
          • Aug 2006
          • 20

          #5
          Thanks for all the replies, Im new in asp.net and uisng C# as my language, can anyone help me to translate the code in c#.

          Thanks

          Comment

          Working...