Mitesh Jain
Mitesh Jain

Reputation: 565

upload file not working in updatepanel

I DONT WANNA GET MY PAGE TO BE GET REFRESH OR POSTBACK

So I am trying uploading file in updatepanel but onclicking upload button the validation check shows that there is no file

my html code is

<asp:UpdatePanel ID="UpdatePanel16" runat="server" UpdateMode="Conditional">
<ContentTemplate>
 <asp:FileUpload ID="fp_upload" runat="server" />&nbsp;&nbsp;
<asp:Button ID="btn_browse" runat="server" Text="Upload" OnClick="btn_browse_Click" />
   </ContentTemplate>  
</asp:UpdatePanel>

It seems to be

enter image description here

my .cs code is

protected void btn_browse_Click(object sender, EventArgs e)
{
   if (fp_upload.HasFile)
    {
        Response.Write("contains file");
    }
    else
    {
       Response.Write("no file");
    }
}

when I used to browse the file and click on upload button every times it goes in else condition. Whats the problem.

ALSO I DONT WANNA GET MY PAGE TO BE GET REFRESH OR POSTBACK

Upvotes: 4

Views: 11465

Answers (3)

Mohammad Arshad Alam
Mohammad Arshad Alam

Reputation: 9862

just add PostBackTrigger after </ContentTemplate> for the FileUploader as below:

 </ContentTemplate>
      <Triggers>
      <asp:PostBackTrigger ControlID="FileUpload1" /> 
     </Triggers>
 </asp:UpdatePanel> 

and add the below code in page load :

ScriptManager.GetCurrent(this).RegisterPostBackControl(FileUpload1);  

or if you want to make it async, you can use this :

<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="btnAsyncUpload" runat="server"
           Text="Async_Upload" OnClick = "Async_Upload_File" />
        <asp:Button ID="btnUpload" runat="server" Text="Upload"
           OnClick = "Upload_File" />               
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID = "btnAsyncUpload"
          EventName = "Click" />
        <asp:PostBackTrigger ControlID = "btnUpload" />
    </Triggers>
</asp:UpdatePanel>

Upvotes: 1

Imad S.
Imad S.

Reputation: 375

To use a FileUpload control inside an UpdatePanel control, set the postback control that submits the file to be a PostBackTrigger control for the panel.

Upvotes: 3

Nagaraj S
Nagaraj S

Reputation: 13484

Write trigger will instruct the button that we are using for the upload to perform a full postback

<asp:UpdatePanel ID="UpdatePanel16" runat="server">
<Triggers>
    <asp:PostBackTrigger ControlID="btn_browse" />
       </Triggers>
<ContentTemplate>
 <asp:FileUpload ID="fp_upload" runat="server" />&nbsp;&nbsp;
<asp:Button ID="btn_browse" runat="server" Text="Upload" OnClick="btn_browse_Click" />
    </ContentTemplate>  
    </asp:UpdatePanel>

Upvotes: 0

Related Questions