Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2010-11-17
11
2,933 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

860 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