简体   繁体   English

PostBack 后如何维护 FileUpload 控件的状态

[英]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).我有多个更新面板(asp:UpdatePanel),并且在每个更新面板中插入数据并显示在相应的网格中(网格也包含在更新面板中)。
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 AutoPostBack="true" .我有一个问题,我有一个 asp:FileUpload 控件,当数据插入这些更新面板时它会被重置,因为很少有控件具有AutoPostBack="true"
I have found one of the closer solution at:-我在以下位置找到了更接近的解决方案之一:-
http://www.codeproject.com/Tips/101834/How-to-Maintain-FileUpload-Control-s-State-after-P 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.不幸的是,所有这三个 if-else 检查都没有通过条件。
I guess that there is some issue related to the UpdatePanel used in parallel with FileUpload control.我猜想与 FileUpload 控件并行使用的 UpdatePanel 存在一些问题。
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. FileUpLoad 在 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.您必须在 updatePanel 中添加一个 asp 按钮来保存所选文件。 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.在单击事件中,将文件名保存在会话中。但要强制完整回发,您必须将触发器添加到 UpdatePanel。 the UpdatePanel should look like this: UpdatePanel 应如下所示:

     <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有关更多信息,您可以在以下 URL 中阅读:http: //www.codeproject.com/Articles/16945/Simple-AJAX-File-Upload

Hope it was helpful...希望对您有所帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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