Get FileUpload control value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • renatois
    New Member
    • Oct 2007
    • 16

    #1

    Get FileUpload control value

    Get FileUpload control value

    Hi guys, I have an aspx webpage with 9 fileupload controls called (fupBlogImg1,fu pBlogImg2,..... ....fupBlogImg9 ). I want to get their values with a for at the code after the button (cmdAlbumCreate ) is clicked.

    Here is the code but fupBlogImg is null (I dont know why)
    for (int n = 1; n < 10; n++)
    {
    string StrBlogImg = "fupBlogImg " + n;
    FileUpload fupBlogImg = (FileUpload)Fin dControl(StrBlo gImg);

    Thank you all
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by renatois
    Get FileUpload control value

    Hi guys, I have an aspx webpage with 9 fileupload controls called (fupBlogImg1,fu pBlogImg2,..... ....fupBlogImg9 ). I want to get their values with a for at the code after the button (cmdAlbumCreate ) is clicked.

    Here is the code but fupBlogImg is null (I dont know why)
    for (int n = 1; n < 10; n++)
    {
    string StrBlogImg = "fupBlogImg " + n;
    FileUpload fupBlogImg = (FileUpload)Fin dControl(StrBlo gImg);

    Thank you all
    Instead of declaring 9 individual file upload controls, consider using an array of 9 file upload controls. Then you will be able to loop through the array as you desire.

    -Frinny

    Comment

    • renatois
      New Member
      • Oct 2007
      • 16

      #3
      Originally posted by Frinavale
      Instead of declaring 9 individual file upload controls, consider using an array of 9 file upload controls. Then you will be able to loop through the array as you desire.

      -Frinny
      Thanks Frinny but how would I do that? please

      Comment

      • renatois
        New Member
        • Oct 2007
        • 16

        #4
        protected void cmdAlbumCreate_ Click(object sender, EventArgs e)
        {
        FileUpload fupBlogImg1 = (FileUpload)Fin dControl("fupBl ogImg1");...... ..

        The code above says fupBlogImg1 is null but I have that control created on the page?? any idea?

        Ty all

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Originally posted by renatois
          protected void cmdAlbumCreate_ Click(object sender, EventArgs e)
          {
          FileUpload fupBlogImg1 = (FileUpload)Fin dControl("fupBl ogImg1");...... ..

          The code above says fupBlogImg1 is null but I have that control created on the page?? any idea?

          Ty all
          Could you please post more of your code so that I can get a better idea of what you mean when you say you've create the control on the page.

          In the mean time check out this article (it's still in a rough draft and the example's in vb. But you should be able to get the idea of how to dynamically create controls through it)

          -Frinny

          Comment

          • renatois
            New Member
            • Oct 2007
            • 16

            #6
            HERE IS THE CODE:

            PAGE.ASPX
            [code=html]
            <asp:FileUplo ad ID="fupBlogImg1 " runat="server" Width="298px" />
            <asp:FileUplo ad ID="fupBlogImg2 " runat="server" Width="298px" />
            ............
            <asp:FileUplo ad ID="fupBlogImg9 " runat="server" Width="298px" />
            <asp:Button ID="cmdAlbumCre ate" runat="server" Text="Inserir imagens no álbum" OnClick="cmdAlb umCreate_Click" />
            [/code]
            PAGE.ASPX.CS
            Code:
            protected void cmdAlbumCreate_Click(object sender, EventArgs e)
            {
            FileUpload fupBlogImg1 = (FileUpload)FindControl("fupBlogImg1");
            FileUpload fupBlogImg2 = (FileUpload)FindControl("fupBlogImg2");
            .............
            FileUpload fupBlogImg9 = (FileUpload)FindControl("fupBlogImg9");
            but fupBlogImg1,2.. .9 is returning null. I have tryied Page.FindContro l but didnt work

            If you need anything else to get a better understanding pls let me know.

            TY
            Last edited by Plater; Jan 21 '08, 10:08 PM. Reason: added [code] tags

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              Originally posted by renatois
              HERE IS THE CODE:

              PAGE.ASPX
              <asp:FileUplo ad ID="fupBlogImg1 " runat="server" Width="298px" />
              <asp:FileUplo ad ID="fupBlogImg2 " runat="server" Width="298px" />
              ............
              <asp:FileUplo ad ID="fupBlogImg9 " runat="server" Width="298px" />
              <asp:Button ID="cmdAlbumCre ate" runat="server" Text="Inserir imagens no álbum" OnClick="cmdAlb umCreate_Click" />
              PAGE.ASPX.CS
              protected void cmdAlbumCreate_ Click(object sender, EventArgs e)
              {
              FileUpload fupBlogImg1 = (FileUpload)Fin dControl("fupBl ogImg1");
              FileUpload fupBlogImg2 = (FileUpload)Fin dControl("fupBl ogImg2");
              .............
              FileUpload fupBlogImg9 = (FileUpload)Fin dControl("fupBl ogImg9");

              but fupBlogImg1,2.. .9 is returning null. I have tryied Page.FindContro l but didnt work

              If you need anything else to get a better understanding pls let me know.

              TY
              You don't need to Redeclare the FileUplaod controls.
              You have declared them in your aspx code and these variables can be accessed in your C# code.

              For instance if you want to retrieve the file name of the file being uploaded in the fupBlogImg9 you would do something like:

              String fileName = fupBlogImg9.Fil eName;

              You don't need to do this:
              FileUpload fupBlogImg9 = (FileUpload)Fin dControl("fupBl ogImg9");
              You already have a fupBlogImg9 FileUpload control to use.

              I think you're losing the object state for your FileUpload control when you are redeclaring it.

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                I rather think that doing this:
                Code:
                FileUpload fupBlogImg9 = (FileUpload)FindControl("fupBlogImg9");
                Is what is causing the problem, you're trying to both reference and re-declare a variable with the same name from two different scopes.

                If you did not want an array, you could do something like this:
                Code:
                FileUpload tempfup =null;
                for(int i=0;i<10;i++)
                {
                   Control c = FindControl("fupBlogImg"+i.ToString());
                   if(c!=null)
                   {//a control was found
                      if(c.GetType()==typeof(FileUpload))
                      {//there are better ways, but I always like to check to make sure
                         tempfup = (FileUpload)c;
                         //now do something with the values of the tempfup object
                      }
                   }
                }

                Comment

                Working...