HtmlInputFile wont work in UpdatePanel

HtmlInputFile.PostedFile=Nothing on postback

Page 1 of 1

10 Replies - 6090 Views - Last Post: 10 August 2010 - 11:26 PM Rate Topic: -----

#1 Sujit P  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 23-February 10

HtmlInputFile wont work in UpdatePanel

Posted 10 August 2010 - 06:15 AM

Hi,
I am using VS-2005.
In my website I have used the html input control with type=file.

The problem is that when I place this control inside an 'UpdatePanel' the 'PostedFile' property becomes 'Nothing' on postback. So in order to get things working I have removed the 'UpdatePanel' and things are working fine.

However, I am interested to know what kind of workaround is required if I were to use an 'UpdatePanel'? In one of the threads at forums.asp.net I found that we need to set a 'PostBackHandler' for the update panel. I have no idea how to implement this. Does it refer to 'AsyncPostbackTrigger' or 'SyncPostBackTrigger'?

Can someone give an idea how to implement this?

Is This A Good Question/Topic? 0
  • +

Replies To: HtmlInputFile wont work in UpdatePanel

#2 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: HtmlInputFile wont work in UpdatePanel

Posted 10 August 2010 - 06:24 AM

You cannot use the input type=file control (or the FileUpload control) with Ajax.
Set the button that uploads the file as a trigger for the UpdatePanel so that it does a full page postback to the server.

-Frinny
Was This Post Helpful? 2
  • +
  • -

#3 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: HtmlInputFile wont work in UpdatePanel

Posted 10 August 2010 - 08:54 AM

@Frinavale: Not sure why you think you can't use the FileUpload control with Ajax, because you most certainly can. I've never used the INPUT control with type "file", so I am not sure about that one.

@Sujit P: Do you have the 'runat="server"' attribute set on the INPUT control?

Post your code so we can see how you have things setup for this project.
Was This Post Helpful? 0
  • +
  • -

#4 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: HtmlInputFile wont work in UpdatePanel

Posted 10 August 2010 - 09:50 AM

@Jayman You cannot use the FileUpload control or the HtmlInputFile control with Ajax (UpdatePanels) because UpdatePanels do not support these controls (See the section on "Controls that Are Not Compatible with UpdatePanel Control")

You can try it if you want to...it will appear to have worked except that the server always receives Null/Nothing for the file.

I have had a lot of issues with this. I had to create custom controls to work around this issue and if they Ever fix it I want to be the first one to know about it. I doubt it will change though because there are security implications to consider with respect to uploading files asynchronously.

-Frinny

This post has been edited by Frinavale: 10 August 2010 - 09:59 AM

Was This Post Helpful? 0
  • +
  • -

#5 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: HtmlInputFile wont work in UpdatePanel

Posted 10 August 2010 - 11:54 AM

Yes you can use a FileUpload control inside an UpdatePanel, I never said it would be an asynchronous postback. So I think that is where your confusion lies.

All you need to do is set a PostBackTrigger in the Triggers for the UpdatePanel and set the ControlID to the ID of the FileUpload control.

I have several portals I maintain where we use a FileUpload control as I just explained, if you need an example.
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: HtmlInputFile wont work in UpdatePanel

Posted 10 August 2010 - 12:00 PM

View PostJayman, on 10 August 2010 - 02:54 PM, said:

Yes you can use a FileUpload control inside an UpdatePanel, I never said it would be an asynchronous postback. So I think that is where your confusion lies.

All you need to do is set a PostBackTrigger in the Triggers for the UpdatePanel and set the ControlID to the ID of the FileUpload control.


Frinavale did suggest that in his first post. I think what he is getting at is that putting the trigger in the UpdatePanel makes it do a full postback to the server, and have the "screen flicker" because it's not asyncronous so it really defeats the purpose of having it in an UpdatePanel.
Was This Post Helpful? 0
  • +
  • -

#7 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: HtmlInputFile wont work in UpdatePanel

Posted 10 August 2010 - 01:18 PM

@Jayman I just wanted to make sure that you, and others who stumble across this thread, realize that you cannot use FileUpload or HtmlInputFile controls within an UpdatePanel and expect it to work with asynchronous/partial-page postbacks to the server.

You didn't mention that the UpdatePanel needed to be configured to cause a full-page-postback in order to upload the file. It would really suck to get someone's hopes up for finding a solution to use a FileUpload/HtmlInputFile control within an UpdatePanel(asynchronously) when there is no way to do this. FileUpload/HtmlInputFile controls require full page postbacks.

To fix this problem I usually use the UpdatePanel's PostBackTrigger to indicate that the button/control that uploads the file should trigger a full page postback (as apposed to an asynchronous/partial-page postback). I did mentioned this before ;)

There are other approaches that I have tried (including an iFrame etc) that will give the smoothness that most people are looking for while satisfying the full page postback requirement. But the easiest way is to simply use the trigger that causes the UpdatePanel to do a full page postback.

There's quite a few articles on the web regarding the topic :)
These articles typically explain the reasoning behind why you can't use an UpdatePanel to asynchronously upload a file to the server too.

-Frinny

This post has been edited by Frinavale: 10 August 2010 - 01:30 PM

Was This Post Helpful? 1
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: HtmlInputFile wont work in UpdatePanel

Posted 10 August 2010 - 01:35 PM

View PostFrinavale, on 10 August 2010 - 04:18 PM, said:

There are other approaches that I have tried (including an iFrame etc) that will give the smoothness that most people are looking for while satisfying the full page postback requirement. But the easiest way is to simply use the trigger that causes the UpdatePanel to do a full page postback.

-Frinny


The easiest way would be just to not put it inside an UpdatePanel since it has no relation to layout of the page. Then you don't have to worry about adding the trigger.
Was This Post Helpful? 1
  • +
  • -

#9 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: HtmlInputFile wont work in UpdatePanel

Posted 10 August 2010 - 01:39 PM

Quite True! :)

I guess I didn't consider that because usually my FileUpload controls are part of a user control or server control. These usually end up getting wrapped up within UpdatePanels in the pages where they are used. I purposefully add an UpdatePanel as a wrapper for the FileUpload control (and the controls that cause the file to actually upload) so that I can specify that it requires a full page postback using triggers.

-Frinny
Was This Post Helpful? 0
  • +
  • -

#10 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: HtmlInputFile wont work in UpdatePanel

Posted 10 August 2010 - 01:39 PM

Sorry Frinavale, I did read right past where you mention setting for a full postback. I guess I just focused on that you said it couldn't be used in an UpdatePanel, which isn't entirely true as we've already established.

All in all I think it made for a good discussion as to the can/cannot of certain controls used inside an UpdatePanel and future readers will certainly benefit from it. :)
Was This Post Helpful? 0
  • +
  • -

#11 Sujit P  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 23-February 10

Re: HtmlInputFile wont work in UpdatePanel

Posted 10 August 2010 - 11:26 PM

View PostFrinavale, on 10 August 2010 - 05:54 PM, said:

You cannot use the input type=file control (or the FileUpload control) with Ajax.
Set the button that uploads the file as a trigger for the UpdatePanel so that it does a full page postback to the server.

-Frinny


Dear Frinavale,
Thanks for your suggestion :-
"Set the button that uploads the file as a trigger for the UpdatePanel so that it does a full page postback to the server".
I first searched for the diff between an AsynPostBackTrigger and PostBackTrigger and found that the later will do a full postback. So set this type of trigger for the upload button and this resolved the issue !

BYE
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1