一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

UpdatePanel上使用FileUpload上传文件代码

时间:2010-07-12 编辑:简简单单 来源:一聚教程网

updatepanel上使用fileupload上传文件代码

@page language="c#" masterpagefile="~/evamanager.master" autoeventwireup="true" codefile="hv_excel.asp教程x.cs" inherits="hv_excel" title="untitled page" %>

 


  组长及联动对象上传
 
   
 
 

   


attachment.aspx


<%@ page language="c#" autoeventwireup="true" codefile="attachment.aspx.cs" inherits="attachment" %>



  无标题页
 



 


 

 
 
 

 



attachment.aspx.cs
using system;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;

public partial class attachment : system.web.ui.page
{
  protected void page_load(object sender, eventargs e)
  {

  }

 


  protected void button1_click(object sender, eventargs e)
  {
  if (fileupload1.hasfile == false)
  {
  scriptmanager.registerstartups教程cript(this.page, this.gettype(), "alert", "alert('未指定上传文件');", true);
  return;
  }
  if (fileupload1.filename.substring(fileupload1.filename.lastindexof('.') + 1, 3).tostring().tolower() != "xls")
  {
  scriptmanager.registerstartupscript(this.button1, this.gettype(), "alert", "alert('文件格式错误!请确保为本页面下载excel!');", true);
  return;
  }

  string savepath = request.physicalapplicationpath + "\upfile\" + fileupload1.filename;

  fileupload1.saveas(savepath);

  }


}

//方法二










而如果你又想在这个updatepanel上做点花样,比如加了一个asp:panel, 可以通过按钮事件触发隐藏或显示的,你会发现fileupload1并不能找到文件。。。

其实道理很简单,updatepanel中的内容是通过xmlhttp实时填充的,在你让他显示之前,查看页面源代码里面是空的。一个动态控件更新普通数据没问题,但上传文件就不行了,我的解决办法是用普通div代替asp:panel,并写了2个函数来动态发送控制脚本,按钮事件中只要调用该函数即可:

private void showpanel()
{
string script = "document.getelementbyid('panel1').style.display='';";
scriptmanager.registerstartupscript(this.page, this.gettype(), "showpanel", script, true);
}
private void closepanel()
{
string script = "document.getelementbyid('panel1').style.display='none';";
scriptmanager.registerstartupscript(this.page, this.gettype(), "closepanel", script, true);
}

 

热门栏目