updatepanel上使用fileupload上传文件代码
@page language="c#" masterpagefile="~/evamanager.master" autoeventwireup="true" codefile="hv_excel.asp教程x.cs" inherits="hv_excel" title="untitled page" %>
<asp:content id="content1" contentplaceholderid="contentplaceholder1" runat="server"><script>
function callback(filename)
{
document.getelementbyid('attach1').innerhtml=filename;
}
</script><fieldset>
<legend>组长及联动对象上传</legend>
<iframe id="file" name="file" src="attachment.aspx"></iframe>
<asp:textbox id="attach1" runat="server"></asp:textbox>
</fieldset>
</asp:content>
attachment.aspx<%@ page language="c#" autoeventwireup="true" codefile="attachment.aspx.cs" inherits="attachment" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script>
window.top.callback(filename);
</script></head>
<body>
<form id="form1" runat="server">
<div>
<asp:fileupload id="fileupload1" runat="server" />
<asp:button id="button1" runat="server" text="ok" onclick="button1_click" />
</div></form>
</body>
</html>
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);
}
}
//方法二
<asp:updatepanel id="updatepanel1" runat="server">
<contenttemplate>
<asp:fileupload id="fileupload1" runat="server" />
<asp:button id="button1" runat="server" text="上传" onclick="button1_click" />
</contenttemplate>
<triggers>
<asp:postbacktrigger controlid="button1" />
</triggers>
</asp:updatepanel>
而如果你又想在这个updatepanel上做点花样,比如加了一个asp:panel, 可以通过按钮事件触发隐藏或显示的,你会发现fileupload1并不能找到文件。。。
其实道理很简单,updatepanel中的内容是通过xmlhttp实时填充的,在你让他显示之前,查看页面源代码里面是空的。一个动态控件更新普通数据没问题,但上传文件就不行了,我的解决办法是用普通div代替asp:panel,并写了2个函数来动态发送控制脚本,按钮事件中只要调用该函数即可:
<div id="panel1"></div>
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);
}