Solved

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

Posted on 2010-11-17
11
2,850 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
 

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now