本文介绍了如何在PostBack上保存FileUpload?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 无法弄清楚这一点。 这是我的代码: <%@ 页 语言 = vb AutoEventWireup = false CodeBehind = WebForm2.aspx.vb 继承 = PDFRequesting.WebForm2 %> < !DOCTYPE html > < html xmlns = http: //www.w3.org/1999/xhtml\"> < head runat = 服务器 > < title > < / title > < / head > < body > < 表格 id = form1 runat = server > < ajaxToolkit:ToolkitScriptManager ID = ToolkitScriptManager1 runat = server > < / ajaxToolkit:ToolkitScriptManager > < div > < asp :标签 ID = Label1 runat = server 文字 = 标签 > < / asp:Label > < asp:TextBox ID = TextBox1 runat = 服务器 > < / asp:TextBox > < ajaxToolkit:AsyncFileUpload ID = AsyncFileUpload1 runat = server / > < asp:按钮 ID = Button1 runat = server 文本 = 按钮 / > < / div > < / form > < / body > < / html > 这是我的代码背后: Imports AjaxControlToolkit 公共 类 WebForm2 继承 System.Web.UI.Page Dim VerifyError As Boolean Pro tected Sub Page_Load( ByVal sender As 对象, ByVal e 作为 System.EventArgs)句柄 我 .Load ' 如果第一次提交页面并且我们在FileUpload控件中有文件而在会话中没有文件 ' 将值存储到SEssion对象 如果((会话( FileUpload23) Nothing ) AndAlso AsyncFileUpload1.HasFile)然后 会话( FileUpload23)= AsyncFileUpload1 Label1.Text = AsyncFileUpload1.FileName ' 下次提交和会话有值,但FileUpload为空白 ' 将值从会话返回到FileUpload ElseIf ((不(会话( FileUpload23 )) Nothing ) AndAlso 不 AsyncFileUpload1.HasFile)然后 AsyncFileUpload1 = CType (会话( FileUpload23),AsyncFileUpload) Label1.Text = AsyncFileUpload1.FileName ' 现在,当Session有文件但用户想要更改文件时,可能会有另一个问题 ' 在这种情况下,我们必须更改会话对象中的文件 ElseIf AsyncFileUpload1.HasFile 然后 会话( FileUpload23)= AsyncFileUpload1 Label1.Text = AsyncFileUpload1.FileName 结束 如果 结束 Sub 受保护的 Sub Button1_Click(发件人作为 对象,e As EventArgs)句柄 Button1.Click 如果验证()然后 否则 Label1.Text = 错误 AsyncFileUpload1 = Session( FileUpload23) 结束 如果 结束 Sub 受保护的 功能验证()作为 布尔 验证= True 如果 TextBox1.Text = 没有 然后 验证= 错误 结束 如果 返回验证 结束 功能 结束 Class 我在网上看过这个例子但是我无法让它发挥作用。每次我点击按钮并找到错误(文本框中没有文本)页面刷新,我在AsyncFileUpload中丢失了我的文件。我尝试过使用旧的FileUpload并得到相同的结果。 我缺少什么? 谢谢解决方案 你需要在OnUploadedComplete事件上保存文件, 参考下面的教程使用ASP.Net AJAX控件工具包AsyncFileUpload控件 [ ^ ] 和CP文章: AsyncFileUpload控件 - Ajax控件工具包中的新控件 [ ^ ] Can't figure this out. Here is my code:<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm2.aspx.vb" Inherits="PDFRequesting.WebForm2" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title></head><body> <form id="form1" runat="server"> <ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></ajaxToolkit:ToolkitScriptManager> <div> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <ajaxToolkit:AsyncFileUpload ID="AsyncFileUpload1" runat="server" /> <asp:Button ID="Button1" runat="server" Text="Button" /> </div> </form></body></html>And here is my code behind:Imports AjaxControlToolkitPublic Class WebForm2 Inherits System.Web.UI.Page Dim VerifyError As Boolean Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 'If first time page is submitted and we have file in FileUpload control but not in session ' Store the values to SEssion Object If ((Session("FileUpload23") Is Nothing) AndAlso AsyncFileUpload1.HasFile) Then Session("FileUpload23") = AsyncFileUpload1 Label1.Text = AsyncFileUpload1.FileName ' Next time submit and Session has values but FileUpload is Blank ' Return the values from session to FileUpload ElseIf ((Not (Session("FileUpload23")) Is Nothing) AndAlso Not AsyncFileUpload1.HasFile) Then AsyncFileUpload1 = CType(Session("FileUpload23"), AsyncFileUpload) Label1.Text = AsyncFileUpload1.FileName ' Now there could be another sictution when Session has File but user want to change the file ' In this case we have to change the file in session object ElseIf AsyncFileUpload1.HasFile Then Session("FileUpload23") = AsyncFileUpload1 Label1.Text = AsyncFileUpload1.FileName End If End Sub Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If Verify() Then Else Label1.Text = "Error" AsyncFileUpload1 = Session("FileUpload23") End If End Sub Protected Function Verify() As Boolean Verify = True If TextBox1.Text = Nothing Then Verify = False End If Return Verify End FunctionEnd ClassI have seen this example on the net but I can't get it to work. Every time I click on the button and an error is found(No Text in the Textbox) the page refreshes and I lose my file in the AsyncFileUpload. I have tried with the old FileUpload and the I get the same results.What am I missing?Thank you 解决方案 you need to save file on OnUploadedComplete event,refer below tutorial Using ASP.Net AJAX Control Toolkits AsyncFileUpload Control[^]and CP article : AsyncFileUpload Control - New Control in Ajax Control ToolKit[^] 这篇关于如何在PostBack上保存FileUpload?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-16 09:16