FileUpload Control in Update Panel using asp.net | Solution to FileUpload control is not working in UpdatePanel.

Introduction: In this article I will
explain with example how to upload Image/ file through File Upload Control that is placed inside Update
Panel in asp.net Ajax using both C# and VB.Net languages. Many of the developers
face a very common problem i.e. “FileUpload control is not working in update
panel in asp.net”. I will also explain the reason and solution of this problem.

Solution: So to solve the issue
we need to set the Button that is uploading the image to be PostBackTrigger instead
of AsyncPostBackTrigger. By doing so the upload button will cause the full post
back and get and retain the image in the FileUpload control whenever clicked
on.

So set it as:

<Triggers>

<asp:PostBackTriggerControlID="btnUpload"/>

</Triggers>

Source Code

Implementation:In the <Form> tag of the design
page (.aspx) places a FileUpload Control and a Button control from the standard
category of the visual studio’s toolbox. Also place ScriptManager from the AJAX
Extension category.

Also create a folder in the root
directory of your project and give it name “Images”. We will store our uploaded
image in this folder. Uploaded images will be prefixed with a random unique name
using the Guid.NewGuid() to avoid the duplicate name problem.

<div>

<fieldsetstyle="width:250px;">

<legend>Upload file example in asp.net</legend>

<asp:ScriptManagerID="ScriptManager1"runat="server">

</asp:ScriptManager>

<table>

<tr>

<td>

<asp:UpdatePanelID="UpdatePanel1"runat="server">

<ContentTemplate>

<asp:FileUploadID="FileUpload1"runat="server"/>

<asp:ButtonID="btnUpload"runat="server"Text="Upload"

onclick="btnUpload_Click"/><br/>

<asp:ImageID="imgShow"runat="server"Width="150px"/>

</ContentTemplate>

<Triggers>

<asp:PostBackTriggerControlID="btnUpload"/>

</Triggers>

</asp:UpdatePanel>

</td>

</tr>

</table>

</fieldset>

</div>

Asp.Net C# Code to upload image through FileUpload Control in Update Panel using Asp.Net

In the code behind
file(.aspx.cs) write the code on the upload button’s click event as:

"If
you like my work; you can appreciate by leaving your comments, hitting Facebook
like button, following on Google+, Twitter, Linked in and Pinterest, stumbling
my posts on stumble upon and subscribing for receiving free updates directly to
your inbox . Stay tuned for more technical updates."

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..
ConversionConversionEmoticonEmoticon