3

Below is the markup page

  <asp:UpdatePanel runat="server" UpdateMode="Conditional">
        <ContentTemplate>
                    <table>
                        <tr>
                            <td>
                                <label>File name<label>
                            </td>
                        </tr>
                       <tr>
                            <td>
  <asp:TextBox runat="server" ID="txtName" Width="150%"></asp:TextBox>
                            </td>
                        </tr>
                      <tr>
                            <td>
                                <label>File</label>
                            </td>
                            <td>
                                <asp:FileUpload runat="server" ID="fileUpload" />
                            </td>
                        </tr>
                    </table>
                    <asp:Button runat="server" ID="btnUpload" Text="Upload file"  OnClick="btnUpload_Click" />
                </div>

                <div class="panel-heading">File display</div>
                <div class="panel-body">
     <asp:GridView runat="server">
       </asp:GridView>
                    <asp:Button runat="server" ID="btnRefresh" Text="Refresh" OnClick="btnRefresh_Click" />
                </div>
            </div>
        </ContentTemplate>
        <Triggers>
            <asp:PostBackTrigger ControlID="btnUpload" />
        </Triggers>
    </asp:UpdatePanel>

With the above code, whenever the user clicks "btnUpload", there'll be a post back, but there is an update panel to prevent the post back, when I change the PostBackTrigger to AsyncPostBackTrigger, the page didn't post back but the fileUpload.HasFile will be false and was unable to get the file.

What's wrong with the above code?Why the postBackTrigger triggers a post back?

User2012384
  • 4,769
  • 16
  • 70
  • 106

2 Answers2

8

File upload will not work with AsyncPostBackTrigger. It will only work with PostBackTrigger.
SOLUTION : If You want to upload file with AsyncPostBackTrigger than you have to use ajaxtoolkit. And in AjaxToolkit their is Control name with "AsyncFileUpload".
Hope You know how to add AjaxToolkit to your Toolbox. If you dont know than tell me i will guide you.

Naisarg Parmar
  • 759
  • 8
  • 25
2

You have not defined event of control, you have to tell that trigger on which event of control:

<asp:AsyncPostBackTrigger ControlID="btnUpload" EventName="Click" />

UPDATE:

you have to use async postback trigger:

AsyncPostBackTrigger - use these triggers to specify a control within or outside of the UpdatePanel that, when clicked, should trigger a partial page postback.

PostBackTrigger - use these triggers to have a control within the UpdatePanel cause a full page postback rather than a partial page postback.

Community
  • 1
  • 1
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • There's an error when trying to add EventName to the PostBackTrigger: Type 'System.Web.UI.PostBackTrigger' does not have a public property named 'EventName'. Also, if I didn't remember wrong, only AsyncPostBack have the EventName attribute... – User2012384 Jun 10 '14 at 07:00
  • yes you need to use AsyncPostBackTrigger if you don't want full postback – Ehsan Sajjad Jun 10 '14 at 07:04
  • But if I use AsyncPostBackTrigger, I won't be able to get the file, is there any way that I can retrieve the file? – User2012384 Jun 10 '14 at 07:06
  • for file uploading you have to do full postback what i experienced – Ehsan Sajjad Jun 10 '14 at 07:08