FileUpload控件回发问题

发布于 2024-08-23 09:17:28 字数 1037 浏览 5 评论 0原文

我在 UpdatePanel 内的 aspx 页面上有一个 FileUpload 控件,其中有一个按钮,单击该按钮我想设置一个带有所选文件的文件名的标签。

这是我的代码:

ASPX PAGE:

<div>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:FileUpload runat="server" ID="fuSimple"></asp:FileUpload>
            <asp:Button runat="server" Text="Button" ID="btnPostback" 
                onclick="btnPostback_Click" />
            <br />
            <asp:Label ID="lblFileName" runat="server" Text="File Name: "></asp:Label>
        </ContentTemplate>
    </asp:UpdatePanel>
</div>

代码隐藏:

protected void btnPostback_Click(object sender, EventArgs e)
    {
        lblFileName.Text = "File Name: " + fuSimple.FileName;  
    }

每次我点击按钮时,我都会得到一个空字符串。我缺少什么?

另外,当我在没有 UpdatePanel 的情况下执行此操作时,它工作得很好。

评论/帮助表示赞赏。

I'm having a FileUpload control on a aspx page inside a UpdatePanel with a button on click of which I want to set a label with the filename of the selected file.

Here is the code I have:

ASPX PAGE:

<div>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:FileUpload runat="server" ID="fuSimple"></asp:FileUpload>
            <asp:Button runat="server" Text="Button" ID="btnPostback" 
                onclick="btnPostback_Click" />
            <br />
            <asp:Label ID="lblFileName" runat="server" Text="File Name: "></asp:Label>
        </ContentTemplate>
    </asp:UpdatePanel>
</div>

Code Behind:

protected void btnPostback_Click(object sender, EventArgs e)
    {
        lblFileName.Text = "File Name: " + fuSimple.FileName;  
    }

Every time I hit the button, I'm getting an empty string. What am I missing?

Also, when I do this without UpdatePanel it works fine.

Comments/help appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹苦笑 2024-08-30 09:17:28

ASP.NET AJAX 和异步回发不支持 FileUpload 控件。由于文件上传工作原理(多部分表单数据)的性质,它们需要完整的回发。

以下问题应该有有用的答案: FileUpload control inside an UpdatePanel 不刷新整个页面?

The FileUpload control is not supported with ASP.NET AJAX and Asynchronous Postbacks. They require a full postback due to the nature of how a file upload works (multipart form data).

The following question should have useful answers: FileUpload control inside an UpdatePanel without refreshing the whole page?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文