保持FileUpload控件值

我在更新面板中有asp.net FileUpload控件。 当我点击上传按钮时,我正在读取一些代码的文件,如果找不到代码,那么我显示ModalPopup从下拉列表中选择用户,否则上传文件并通过电子邮件发送给该代码的用户(此代码保存在数据库中) 。 如果找不到代码,它显示ModalPopup并删除所选文件,我想在回发后保留所选文件。 这是我的代码

      

并点击按钮

 protected void btnupload_Click(object sender, EventArgs e) { //Reading the file and Checking from Database if(codefound) { //Sending email to the user of the Code } else { ModalPopupExtender1.Show(); } } 

如何在post后面保留上传控件的值?

背景::当使用FileUpload Control选择文件时,然后在回发时,使用文件的HttpPostedFile对象初始化PostedFile属性。 由于http请求无法维护状态,因此它失去了它的状态。

注意: FileUpload控件不能用于异步回发。因此需要回发来获取文件。 一种方法是设置上传按钮的触发器,即 &NOT

           

和你的上传按钮代码:

  protected void btnUpload_Click(object sender, EventArgs e) { if (fileUpload1.HasFile) { fileName = fileupload1.FileName; fileUpload1.SaveAs("~/UploadedContent/" + fileName); } } 

为了支持FILEUPLOAD CONTROL的值 ,您可以在会话中完全存储fileupload对象,并在回发后从会话中检索所需的值。

 protected void Page_Load(object sender, EventArgs e) { // store the FileUpload object in Session. // "FileUpload1" is the ID of your FileUpload control // This condition occurs for first time you upload a file if (Session["FileUpload1"] == null && FileUpload1.HasFile) { Session["FileUpload1"] = FileUpload1; Label1.Text = FileUpload1.FileName; // get the name } // This condition will occur on next postbacks else if (Session["FileUpload1"] != null && (! FileUpload1.HasFile)) { FileUpload1 = (FileUpload) Session["FileUpload1"]; Label1.Text = FileUpload1.FileName; } // when Session will have File but user want to change the file // ie wants to upload a new file using same FileUpload control // so update the session to have the newly uploaded file else if (FileUpload1.HasFile) { Session["FileUpload1"] = FileUpload1; Label1.Text = FileUpload1.FileName; } } 

这个问题有点记录,更新面板列为不使用某些控件。

文件上传和树视图是2个biggies。

要使其工作,您应该使用Triggers / PostbackTrigger

           

尝试添加

 $('form').attr('enctype', 'multipart/form-data');