简体   繁体   中英

How to Maintain FileUpload Control’s State after PostBack

I have got multiple Update Panels(asp:UpdatePanel) and in each of those update panels data is inserted and shown in the corresponding grids(grids too include in update panels).
I have the problem that I have a asp:FileUpload Control which is reset when data is inserted in those update panels since few controls have .
I have found one of the closer solution at:-
http://www.codeproject.com/Tips/101834/How-to-Maintain-FileUpload-Control-s-State-after-P

        if (Session["FileUpload1"] == null && theFile.HasFile)
        {
            Session["FileUpload1"] = theFile;
            lblStatus.Text = theFile.FileName;
        }
        else if (Session["FileUpload1"] != null && (!theFile.HasFile))
        {
            theFile = (FileUpload)Session["FileUpload1"];
            lblStatus.Text = theFile.FileName;
        }
        else if (theFile.HasFile)
        {
            Session["FileUpload1"] = theFile;
            lblStatus.Text = theFile.FileName;
        }


But this solution is not resolving my problem. Unfortunately all these three if-else checks are not passing the condition.
I guess that there is some issue related to the UpdatePanel used in parallel with FileUpload control.
I have searched a lot of articles, but it could not find the resolution. Kindly help me in this regards at earliest.

我有同样的问题,并通过在页面加载事件中添加以下行来解决:

Page.Form.Attributes.Add("enctype", "multipart/form-data");

You are right! FileUpLoad does not work in UpdatePanel. You must force full postback to make it works. you have to add an asp button in the updatePanel to save the selected file. in the click event save the fileName in the session.. but also to force full post back you have to add trigger to the UpdatePanel. the UpdatePanel should look like this:

     <asp:UpdatePanel ID="UpdatePanel4" runat="server">
        <ContentTemplate>
            <asp:FileUpload ID="FileUpload1" runat="server"/>
            <asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="Button" />
        </ContentTemplate>
        <Triggers>
            <asp:PostBackTrigger  ControlID="Button3" />
        </Triggers>
    </asp:UpdatePanel>

for more info you can read in the following URL: http://www.codeproject.com/Articles/16945/Simple-AJAX-File-Upload

Hope it was helpful...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM