File upload control fails first time, then works on subsequent submits

As everybody knows that FileUpload control doesn’t work inside update panel, there are some work around and in fact I have posted post about that as well.   Here is my post link just in case you got here and wants to know more about that.

I have file upload control in user control and have update panel in aspx page so I can’t really add postbacktrigger in ascx page because I don’t have control on updatepanel.  So I have to specify in code behind to do full postback.   Here is the code that I have put in code behind.

ScriptManager.GetCurrent(Page).RegisterPostBackControl(btnUpload);

But I was facing some weird issue.   When I submit the page first time, file upload control’s PostedFile(Filebytes/fileName) property returns nothing.  But second time and subsequent submit, file upload controls works fine and  PostedFile property returns proper value.  Isn’t it weird? (I think so…)

To fix this weird issue all you need to do is set encoding type to “multipart/form-data”.  You can set it from code behind using

Page.Form.Enctype = “multipart/form-data”;

Hope this helps.  If this post really helps you, please click the Google +1 button to show it really helps you save your time.

Posted in Microsoft Technology Tagged with:
7 comments on “File upload control fails first time, then works on subsequent submits
  1. Alex says:

    Excellent!!!
    I read hundreds of pages without success, is the only instruction that worked for me.
    Tnx!!!

  2. Kay says:

    Thank you! It’s working.

  3. Heraclea says:

    You actually make it seem so easy with your presentation but I find this topic to be really
    something that I think I would never understand. It seems too complex and extremely broad for me.
    I am looking forward for your next post, I will try to get
    the hang of it!

  4. Ramakrishna says:

    Valuable time saved, of course spent a day searching for solution every thing half explained.

  5. Tanzeelur Rehman says:

    Thank you very much sir. It really worked for me.

    Why this happens, what is the problem that it doesn’t work for the first time.

  6. Jane says:

    I love this

  7. Padmini says:

    Where should I put the code in code behind ? On page load event of user control or the click event of the button?

1 Pings/Trackbacks for "File upload control fails first time, then works on subsequent submits"
  1. File upload control fails first time, then works on subsequent ……

    […]I have file upload control in user control and have update panel in aspx page so I can’t really add postbacktrigger in ascx page because I don’t have control on updatepanel. So I have to specify in code behind to do full postback. … Howto : Pa…

Ads