首先我很遗憾的告诉大家,因为微软的偷懒,目前UpdatePanel还不支持文件上传。变相的解决办法就是UpdatePanel中设置PostBackTrigger:
<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);
}
<ContentTemplate>
<asp:FileUpload ID= "FileUpload1" runat= "server" />
<asp:Button ID= "Button1" runat= "server" Text= "上传" οnclick= "Button1_Click" /> </ContentTemplate>
</asp:UpdatePanel>



本文出自 “lee” 博客,请务必保留此出处http://leehai.blog.51cto.com/757045/153736
本文出自 51CTO.COM技术博客1:在适当的位置,放置一个上传附件的UpdatePanel区域


<asp:Image ID="img_photo" runat="server" Height="64" ImageUrl="~/images/anonymous.gif"
Width="64" /><br />
<input type="hidden" runat="server" id="hi_src" name="hi_src" value="~/images/anonymous.gif" />







{
string fileFullPath = fu_photo.PostedFile.FileName;
string fileName = fileFullPath.Substring(fileFullPath.LastIndexOf('\\') + 1);
string fileSavePath = "../Photos/" + fileName;
fu_photo.PostedFile.SaveAs(Server.MapPath(fileSavePath));
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "photoscript", "window.top.callBack('" + fileSavePath + "');", true);
用js改变图片路径为新上传的路径,然后服务器端获的隐藏字段的值,即为新上传图片路径
上传页面时不能获得js更改后的image控件的属性值,所以添加一个隐藏字段。。。

function callBack(fileName)
{
document.getElementById('<%=img_photo.ClientID %>').src=fileName;
document.getElementById('<%=hi_src.ClientID %>').value=fileName;
}
