FileUpload放在UpdatePanl中不好使,在网上找到两种方法,经测试,第二种可以使用!第一种没试,呵! 方法一: 曾在开发ATLAS时候,想用UpdatePanel (UP)来上传文件,但是没有想到FileUpload (FU)控件不能在UP里使用,这里有个小技巧,可以让你的FU控件在UP里面起做用. 来看代码:
<
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 >
Protected
Sub
Page_Load()
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() 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
方法二: 发现把FileUpload控件放到ASP.NET AJAX UpdatePanl中,事件倒是能捕捉到,但FileUpload在后台的HasFile老是为False,也就是说文件根本没传到服务端,用Google查了查,发现ASP.NET AJAX官方文档已经说明了FileUpload和AJAX不兼容! 办法的:
protected
void
DatePicker1_SelectionChanged(
object
sender, EventArgs e)
{ Label1.Text = DatePicker1.DateValue.ToShortDateString(); } protected void Button1_Click( object sender, EventArgs e) { if (FileUpload1.HasFile) { Label1.Text = FileUpload1.FileName; } }
It is at this point that we experience the problem. Run the form and you will find that the file upload control does not work. Because the file upload control is within an update panel the file is not posted to the server. As mentioned earlier the trick is to force the file upload control to perform a full postback, and we do this using triggers. Triggers allow the developer to specify what will cause partial and full postbacks. They must be defined within the UpdatePanel but outside of the ContentTemplate. We want to create a trigger that will instruct the button that we are using for the upload to perform a full postback. The updated markup is:
<
asp:UpdatePanel
ID
="UpdatePanel1"
runat
="server"
UpdateMode
="conditional"
>
< Triggers > < asp:PostBackTrigger ControlID ="Button1" /> </ Triggers > < ContentTemplate > < ews:DatePicker ID ="DatePicker1" runat ="server" UsingUpdatePanel ="True" OnSelectionChanged ="DatePicker1_SelectionChanged" />< br /> < asp:Label ID ="Label1" runat ="server" ></ asp:Label >< br />< br /> < asp:FileUpload ID ="FileUpload1" runat ="server" /> < asp:Button ID ="Button1" runat ="server" Text ="Upload" OnClick ="Button1_Click" /> </ ContentTemplate > </ asp:UpdatePanel >
|