I'm aware of the fact that the FileUpload control is not compataible with asyc postbacks, and that the way to use the FileUpload control inside an UpdatePanel is to create a PostBack trigger that causes a full postback when the button you want is submitted,
as shown below.
But what do you do when you are using a FormView or other such template based control? When using a template based control, like FormView, the control that you needs to create the trigger for doesn't really exist, so you can't just create a trigger and set
the ControlID to the ID of the button in the FormView, because that button doesn't always exist. So, how do you deal with this?
I struggled with this compatibility issue. I use a file upload to upload pictures. Below is my code, I hope it helps. It is used in the same situation as what you described, an UpdatePanel contained in a FormView.
Protected Sub ImageButton1_Click(ByVal sender
As Object,
ByVal e As System.Web.UI.ImageClickEventArgs)Dim fuTmp
As FileUpload =
CType(FormView12.FindControl("fuPictures"), FileUpload)
If Not fuTmp.PostedFile
Is Nothing
And fuTmp.PostedFile.ContentLength > 0
Then
'Declare file names/paths
Dim strFileExt
As String = System.IO.Path.GetExtension(fuTmp.PostedFile.FileName)
Dim strFileName
As String = System.Guid.NewGuid().ToString()
Dim SaveLocation
As String =
"~/Pictures/" & strFileName & strFileExtDim SaveLocationThumb
As String =
"~/Pictures/" & strFileName &
"-Thumb" & strFileExt
Dim strSaveLocationDB
As String =
"http://server/Pictures/" & strFileName & strFileExt
Dim strSaveLocationThumbDB
As String =
"http://server/Pictures/" & strFileName &
"-Thumb" & strFileExt
Dim imgTmp As System.Drawing.Image = System.Drawing.Image.FromStream(fuTmp.FileContent)
Dim imgLarge
As System.Drawing.Image = GlobalFunctions.ResizeImage(imgTmp, 700, 700)
Dim imgThumb As System.Drawing.Image = GlobalFunctions.ResizeImage(imgTmp, 200, 200)
Thank you, but none of the posted solutions, as far as I can tell, address my question.
The issue is having a FileUpload control, along with other controls, inside an FormView, which is inside an UpdatePanel.
The IFrame solution, as far as I can tell, doesn't work in this situation, because I can't put the IFrame inside the FormView, inside the UpdatePanel, and even if I could, I couldn't reference the controls by name as the examples show because they are inside
a FormView.
As for the first suggestion, it is not the same as my situation. My situation is not an UupdatePanel inside a FormView, it is a FormView inside an UpdatePanel. In this example the FileUpload works without any fancy tricks because it is outside the UpdatePanel.
What I am looking for is a way to use a FileUpload field that, along with many other fields, is part of a FormView, which is inside an Ajax TabContainer, which is inside an UpdatePanel.
As far as I can see, neither the PostBackTrigger method nor the IFrame method works in this situation. Perhaps there is a way to create the proper PostBackTrigger at run time, after during one of the events in the FormView, but I'm not sure if that is possible,
or hwo to do it.
malachi151
Member
46 Points
114 Posts
Using a FileUpload inside a FormView inside an UpdatePanel (help!)
Jan 17, 2008 04:16 PM|LINK
I'm aware of the fact that the FileUpload control is not compataible with asyc postbacks, and that the way to use the FileUpload control inside an UpdatePanel is to create a PostBack trigger that causes a full postback when the button you want is submitted, as shown below.
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="conditional"><Triggers><asp:PostBackTrigger ControlID="Button1" /></Triggers>
...
But what do you do when you are using a FormView or other such template based control? When using a template based control, like FormView, the control that you needs to create the trigger for doesn't really exist, so you can't just create a trigger and set the ControlID to the ID of the button in the FormView, because that button doesn't always exist. So, how do you deal with this?
msnyder0
Member
558 Points
298 Posts
Re: Using a FileUpload inside a FormView inside an UpdatePanel (help!)
Jan 17, 2008 07:10 PM|LINK
I struggled with this compatibility issue. I use a file upload to upload pictures. Below is my code, I hope it helps. It is used in the same situation as what you described, an UpdatePanel contained in a FormView.
Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)Dim fuTmp As FileUpload = CType(FormView12.FindControl("fuPictures"), FileUpload)
If Not fuTmp.PostedFile Is Nothing And fuTmp.PostedFile.ContentLength > 0 Then 'Declare file names/paths Dim strFileExt As String = System.IO.Path.GetExtension(fuTmp.PostedFile.FileName) Dim strFileName As String = System.Guid.NewGuid().ToString() Dim SaveLocation As String = "~/Pictures/" & strFileName & strFileExtDim SaveLocationThumb As String = "~/Pictures/" & strFileName & "-Thumb" & strFileExt
Dim strSaveLocationDB As String = "http://server/Pictures/" & strFileName & strFileExt Dim strSaveLocationThumbDB As String = "http://server/Pictures/" & strFileName & "-Thumb" & strFileExt
Dim imgTmp As System.Drawing.Image = System.Drawing.Image.FromStream(fuTmp.FileContent) Dim imgLarge As System.Drawing.Image = GlobalFunctions.ResizeImage(imgTmp, 700, 700) Dim imgThumb As System.Drawing.Image = GlobalFunctions.ResizeImage(imgTmp, 200, 200)
TryimgLarge.Save(Server.MapPath(SaveLocation), imgTmp.RawFormat)
imgThumb.Save(Server.MapPath(SaveLocationThumb), imgTmp.RawFormat)
'Query to Insert into DB CType(FormView12.FindControl("tbPictureDescription"), TextBox).Text = Nothing Catch Exc As ExceptionResponse.Write(
"Error: " & Exc.Message) End Try End If
CType(FormView12.FindControl("gvPictures"), GridView).DataBind() CType(FormView12.FindControl("upPictures"), UpdatePanel).Update() End Subchetan.sarod...
All-Star
65739 Points
11138 Posts
Re: Using a FileUpload inside a FormView inside an UpdatePanel (help!)
Jan 18, 2008 02:26 AM|LINK
FileUpload control are not compatible with UpdatePanel when it is used to upload files as part of an asynchronous postback.
http://www.asp.net/ajax/documentation/live/overview/UpdatePanelOverview.aspx
See here building AJAX enabled File Upload control
http://aspalliance.com/1442_Building_AJAX_Enabled_File_Uploading_System_with_Progress_Bar_Using_ASPNET_20.all
http://geekswithblogs.net/rashid/archive/2007/08/01/Create-An-Ajax-Style-File-Upload.aspx
http://msmvps.com/blogs/luisabreu/archive/2006/12/14/uploading-files-without-a-full-postback.aspx
http://blogs.infragistics.com/blogs/tony_lombardo/archive/2007/04/09/file-uploads-where-s-the-ajax.aspx
http://www.codeproject.com/KB/aspnet/AJAXUpload.aspx
Senior Software Engineer,
Approva Systems Pvt Ltd, Pune, India.
malachi151
Member
46 Points
114 Posts
Re: Using a FileUpload inside a FormView inside an UpdatePanel (help!)
Jan 21, 2008 07:49 PM|LINK
Thank you, but none of the posted solutions, as far as I can tell, address my question.
The issue is having a FileUpload control, along with other controls, inside an FormView, which is inside an UpdatePanel.
The IFrame solution, as far as I can tell, doesn't work in this situation, because I can't put the IFrame inside the FormView, inside the UpdatePanel, and even if I could, I couldn't reference the controls by name as the examples show because they are inside a FormView.
As for the first suggestion, it is not the same as my situation. My situation is not an UupdatePanel inside a FormView, it is a FormView inside an UpdatePanel. In this example the FileUpload works without any fancy tricks because it is outside the UpdatePanel.
What I am looking for is a way to use a FileUpload field that, along with many other fields, is part of a FormView, which is inside an Ajax TabContainer, which is inside an UpdatePanel.
As far as I can see, neither the PostBackTrigger method nor the IFrame method works in this situation. Perhaps there is a way to create the proper PostBackTrigger at run time, after during one of the events in the FormView, but I'm not sure if that is possible, or hwo to do it.