Solved

FileUpload in C# does not get file when click Upload button

Posted on 2010-11-17
11
2,948 Views
Last Modified: 2013-12-17
I have coded an asp page into a site now using a master page.  The FileUpload control worked fine on a regular asp page when clicking a button but when added into site it does not work.  I've recently inherited this site to build and revise so I'm still getting familiar with it's inner workings.


I've added the following code just to the .aspx page in the site - this does display the message when clicking the uploadbutton after selecting a file to upload.  I do get the "Nothing loads!" message and the FileUpload path that was browsed is gone.

        <script runat="server" type="text/c#">
            protected void Button1_Click(object sender, EventArgs e)
            {
         
                if (FileUpload1.HasFile)
                {
                    lblMessage.Text = FileUpload1.PostedFile.FileName;
                }
                else
                {
                    lblMessage.Text = "Nothing loads!";
                }
            }
          </script>

Are there additional permissions or validations I will need to code for to make this upload a file?

Any suggestions are welcome?

Thanks in advance.
0
Comment
Question by:Shay10
  • 5
  • 5
11 Comments
 
LVL 11

Expert Comment

by:jasonduan
ID: 34161687
Is the FileUpload inside an UpdatePanel?
0
 

Author Comment

by:Shay10
ID: 34161724
It was - I've moved it out thinking that might cause it to lose the path when clicking upload button but it still acts the same.
0
 
LVL 11

Expert Comment

by:jasonduan
ID: 34163930
Did you check the master page? Make sure there is no UpdatePanel that wraps the ContentPanel.
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

Author Comment

by:Shay10
ID: 34164578
Just checked this and there IS an UpdatePanel around the ContentPlaceHolder.  Can I remove this without breaking the whole site or what change will I need to make to make my page work in this environment?  

Would it be easier to just make a new master page?

Thanks
0
 

Author Comment

by:Shay10
ID: 34165094
Need help with this - fast as possible as deadline is fast approaching.  I've increased the points!
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34165732
There are some considerations when using a FileUpload inside an UpdatePanel, this document http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.aspx has a section specifically addressing that (search the page for "Using the FileUpload Control with the UpdatePanel Control").

The AsyncFileUpload in the AJAX control toolkit (http://www.asp.net/ajax) might be easier to use in an UpdatePanel.

I would expect that removing the UpdatePanel from the master page would in fact break other things (though that seems like it might be a poor design choice).
0
 
LVL 11

Accepted Solution

by:
jasonduan earned 500 total points
ID: 34165950
0
 

Author Comment

by:Shay10
ID: 34168359
Have just tried tgerbert suggestion and got this:


Control with ID 'FileUpload1' being registered through RegisterAsyncPostBackControl or RegisterPostBackControl must implement either INamingContainer, IPostBackDataHandler, or IPostBackEventHandler.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Control with ID 'FileUpload1' being registered through RegisterAsyncPostBackControl or RegisterPostBackControl must implement either INamingContainer, IPostBackDataHandler, or IPostBackEventHandler.

Source Error:


Line 450:    public void RegisterPostbackTrigger(FileUpload triggerOn)
Line 451:    {
Line 452:        This_Master.RegisterPostBackControl(triggerOn);
Line 453:    }
Line 454:
 

Source File: c:\I>PATH>s\This_MasterPage.master.cs    Line: 452

Stack Trace:


[ArgumentException: Control with ID 'FileUpload1' being registered through RegisterAsyncPostBackControl or RegisterPostBackControl must implement either INamingContainer, IPostBackDataHandler, or IPostBackEventHandler.]
   System.Web.UI.PageRequestManager.RegisterPostBackControl(Control control) +398890
   System.Web.UI.ScriptManager.RegisterPostBackControl(Control control) +21
MasterPages_Common_MasterPage.RegisterPostbackTrigger(FileUpload triggerOn) in c:\I>PATH>s\This_MasterPage.master.cs:452
   LeadManagemtSystem.Page_Load(Object sender, EventArgs e) in c:\I>PATH>s\ThisPage.aspx.cs:89
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +50
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627


 
0
 
LVL 11

Expert Comment

by:jasonduan
ID: 34168532
Read the suggested post carefully, it registers the button that causes the postback, NOT the FileUpload itself.
0
 

Author Closing Comment

by:Shay10
ID: 34178034
Now that the crisis is over, I can now take some time to give credit where it is due.

First of all, I had mixed up the names when replying earlier to a post.  It was actually your link I was reading through when I replied earlier and not tgerbert.

After going through this link it really was easy - and your correction pointed out just exactly why I received the error message I posted.

I lost much coding time on this issue - 2 whole days!!

Thanks for taking the time to reply - you really helped me out as I made my deadline and I have learned much from this issue and your help.

Thanks again!!!

0
 
LVL 11

Expert Comment

by:jasonduan
ID: 34178147
Glad i can help. Happy programming!
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question