在UpdatePanel上使用FileUpload上传文件(转)

转 :http://blog.orgact.com/leisang/archive/2007/10/19/00000001.html
首先我很遗憾的告诉大家,因为微软的偷懒,目前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); }
posted @ 2008-06-04 22:06  单车骑客  阅读(1007)  评论(0编辑  收藏  举报