locked
Using a FileUpload inside a FormView inside an UpdatePanel (help!) RRS feed

  • Question

  • User-1093115277 posted

    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><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p><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?

    Thursday, January 17, 2008 12:16 PM

All replies

  • User1384525187 posted

    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)

     

    Try

    imgLarge.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 Exception

    Response.Write("Error: " & Exc.Message)

    End Try

    End If

     

    CType(FormView12.FindControl("gvPictures"), GridView).DataBind()

    CType(FormView12.FindControl("upPictures"), UpdatePanel).Update()

    End Sub

    Thursday, January 17, 2008 3:10 PM
  • User-1093115277 posted

    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.

    Monday, January 21, 2008 3:49 PM