Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3291
  • Last Modified:

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

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
Shay10
Asked:
Shay10
  • 5
  • 5
1 Solution
 
jasonduanCommented:
Is the FileUpload inside an UpdatePanel?
0
 
Shay10Author Commented:
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
 
jasonduanCommented:
Did you check the master page? Make sure there is no UpdatePanel that wraps the ContentPanel.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Shay10Author Commented:
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
 
Shay10Author Commented:
Need help with this - fast as possible as deadline is fast approaching.  I've increased the points!
0
 
Todd GerbertIT ConsultantCommented:
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
 
Shay10Author Commented:
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
 
jasonduanCommented:
Read the suggested post carefully, it registers the button that causes the postback, NOT the FileUpload itself.
0
 
Shay10Author Commented:
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
 
jasonduanCommented:
Glad i can help. Happy programming!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now