:: 首页  ::  :: 联系 :: 订阅 订阅  :: 管理

updatepanel中不能使用fileupload的弥补方法

FileUpload放在updatepanel中,回调时发现上其属性hasfile为false。解决方案如下:
 

方案一、如果将scriptmanager的enablepartialrending设为false则可以正确上传,这个方法最简单,但是会有缺陷,就是在同一个页面上的多个UpdatePanel不可以独自刷新了。另外,当你的UpdatePanel中存在Validator的话,会造成整个页面postback,这个问题似乎是Altas的一个bug.

方案二、摘自http://www.netfocus.cn/article950.html

1:主页面中使用UpdatePanel,然后UpdatePanel里面放置的不是FileUpload控件,而是一个Iframe
2:这个iframe在链接一个新的页面,那个页面里面有FileUpload控件。
3:上传完毕后,告诉主页面上传得结果
 
直接使用FileUpload,服务端是无法找到上传文件的。
            <atlas:UpdatePanel ID="up1" Mode="Conditional" runat="server">
                
<ContentTemplate>
                    
<asp:FileUpload ID="fu_photo" runat="server" />
                    
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                 
</ContentTemplate>
            
</atlas:UpdatePanel>

如何实现
1:新建主页面Default.aspx
      在适当的位置,放置一个上传附件的UpdatePanel区域
     
            <atlas:UpdatePanel ID="up_attachment" Mode="Conditional" runat="server">
                
<ContentTemplate>
                        <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" />
                   <iframe id="file" name="file" src="attachment.aspx"></iframe>
                
</ContentTemplate>
            
</atlas:UpdatePanel>
2:新建上传文件的页面attachment.aspx,然后放上FileUpload控件
<div>
    
<asp:FileUpload ID="FileUpload1" runat="server" />
          
<asp:Button ID="Button1" runat="server" Text="OK" OnClick="Button1_Click" />
    
</div>
3:在attachment.aspx里面,上传文件之后调用主页面的js:
    protected void Button1_Click(object sender, EventArgs e)
    {
        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);
4:Default.aspx主页面里面增加这个函数,处理返回值
用js改变图片路径为新上传的路径,然后服务器端获的隐藏字段的值,即为新上传图片路径

上传页面时不能获得js更改后的image控件的属性值,所以添加一个隐藏字段。。。
    <script>
     function callBack(fileName)
     {
        document.getElementById('<%=img_photo.ClientID %>').src=fileName;
        document.getElementById('<%=hi_src.ClientID %>').value=fileName;
     }

    
</script>
posted on 2009-06-13 12:44  Qzzm!  阅读(1406)  评论(0)    收藏  举报