Friday, March 20, 2009

asp:FileUpload control in Modal Popup Extender

Hello guys !

Let's go straight to the issue this time.
I have a Modal Popup Extender and want to use the standard FileUpload control in it. What I mean by saying the "standard FileUpload" control is that I will use the asp:FileUpload control and not the AJAX-enabled one which is in the AJAX Control Toolkit.

What's the issue/problem?
The problem is that the AJAX makes partial updates and doesn't perform a full postback. The FileUpload control itself works with full postback - in the postback the File which you uploaded is attached in the Request.Files collection. So, when you are not performing a full postback, the file is not uploaded in the Request.Files collection and you cannot get the file to use it - for saving in the database for example.

The solution
The Script Manager has one very useful method - RegisterPostBackControl ( http://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.registerpostbackcontrol.aspx ). It basically registers a control, which should implicitly do a full postback. In my case, it was enough just to register the FileUpload holding control to perform a full postback and I had the File uploaded into the Request.Files and ready for use.


Conclusion
The issue and the solution were pretty straight-forward this time. But it can become trickier, if you don't know the RegisterPostBackControl method.

7 comments:

Anonymous said...

HOW??

Anonymous said...

Are you sure that this method works?

cypressx said...

Yes, I am sure that the method works.

Just register the control you have placed in your ModalPopupExtender in the page/control containing the ModalPopupExtender and it should work.

Anonymous said...

I would really like to see this working, a user control with FileUpload inside a modal popup. Do you have an actual working code?

marius09 said...

It could be another approach also, if all is embedded in an UpdatePanel.
It should use section:




However, using ScriptManager is a better approach, especially for complex structures (for example: UpdatePanel contains a TabContainer, which contains "xxx" control)

Anonymous said...

Trying to register a file upload control yields the following exception:

An exception of type 'System.ArgumentException' occurred in System.Web.Extensions.dll but was not handled in user code

Additional information: Control with ID 'uxAddFile' being registered through RegisterAsyncPostBackControl or RegisterPostBackControl must implement either INamingContainer, IPostBackDataHandler, or IPostBackEventHandler.

You can register button controls but not file upload controls. An example of working code instead of random guesses would be helpful.

viagra said...

In principle, a good happen, support the views of the author