Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-11-17
11
Medium Priority
?
3,152 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

885 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