曾在开发ATLAS时候,想用UpdatePanel (UP)来上传文件,但是没有想到FileUpload (FU)控件不能在UP里使用,这里有个小技巧,可以让你的FU控件在UP里面起做用. 来看代码:
HTML:
<div>
<atlas:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true">
</atlas:ScriptManager>
<atlas:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" /><asp:Button ID="cmdButton1" runat="server"
Text="Upload" /><asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</ContentTemplate>
</atlas:UpdatePanel>
<asp:Button ID="cmdButton2" OnClick="cmdButton2_click" runat="server" Text="Full post back" />
</div>
<atlas:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true">
</atlas:ScriptManager>
<atlas:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" /><asp:Button ID="cmdButton1" runat="server"
Text="Upload" /><asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</ContentTemplate>
</atlas:UpdatePanel>
<asp:Button ID="cmdButton2" OnClick="cmdButton2_click" runat="server" Text="Full post back" />
</div>
CODE BEHIND:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.cmdButton1.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(Me.cmdButton2, ""))
End Sub
Protected Sub cmdButton2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
If Me.FileUpload1.HasFile Then
System.Threading.Thread.Sleep(1000)
Me.Label1.Text = Me.FileUpload1.FileName
End If
End Sub
Me.cmdButton1.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(Me.cmdButton2, ""))
End Sub
Protected Sub cmdButton2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
If Me.FileUpload1.HasFile Then
System.Threading.Thread.Sleep(1000)
Me.Label1.Text = Me.FileUpload1.FileName
End If
End Sub
这样的话,当你在点击UP里面的UPLOAD按钮时,整个页面就会回传,当然你的可以找到你FU控件里面的文件,所以上传文件是没问题. OK~
P.S. 虽然解决这个问题,但是那个多余的BUTTON控件和整个页面的回传都是瑕疵. 当然希望ATALS团队在开发的时候,能够考虑到这一点,给我们开发者带来更多的惊喜!
P.S. Atlas关于UpdatePanel的使用说明--原文链接