Hi,

I have a gridview that has a fileupload (fupRespuesta) control in a itemtemplate field, I need to enable automatic upload after file is selected.

Then I added a imageButton (imgRespuesta) in the template field and added its event with addhandler...

Well my problem is that when I uncomment the red line, the event of the image button don't fire (I can't understand why)

I need it work, or some other idea to do it.

Heres my code:

Private Sub gv_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvPropuestasDiagnosticoListar.RowCreated

Dim imgRespuesta As New ImageButton
        Dim fupRespuesta As New FileUpload
        imgRespuesta = e.Row.FindControl("imgProDiagUploadRespuesta")
        fupRespuesta = e.Row.FindControl("fupvcProDiaRutaRespuesta")
        If Not imgRespuesta Is Nothing And Not fupRespuesta Is Nothing Then
            AddHandler imgRespuesta.Click, AddressOf img_Click
            'imgRespuesta.Attributes.Add("Style", "Display:none")
            'fupRespuesta.Attributes.Add("onchange", "return document.getElementById('" + imgRespuesta.ClientID + "').click();")
        End If
End Sub

Private Sub img_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        Dim fup As FileUpload
        Dim img As ImageButton
        fup = CType(sender.Parent.Parent, GridViewRow).FindControl("fupvcProDiaRutaPropuesta")
        img = CType(sender.Parent.Parent, GridViewRow).FindControl("imgProDiagUploadPropuesta")
        If fup.HasFile = True Then
            img.Attributes.Remove("Style")
            Dim fullpath = IO.Path.GetFullPath(fup.FileName)
            Dim name = IO.Path.GetFileName(fullpath)
            fup.SaveAs(Server.MapPath("~") + "\Documentos\" & name.ToString)
        End If
    End Sub

Dani AI

Generated

Short version: the event wiring that suggested will correctly fire your ImageButton handler, but a browser/ASP.NET restriction is why FileUpload.HasFile comes back false: file inputs are not sent during an asynchronous (UpdatePanel-style) postback. As discovered, the fix is to make the upload cause a full postback (or use a control/technique that explicitly supports AJAX uploads).

Force a full postback for the button (two common ways):

  • Add a PostBackTrigger for the ImageButton in the UpdatePanel triggers.
  • Or register the ImageButton as a postback control at runtime with ScriptManager.

Example (VB: register dynamically in RowDataBound):

If e.Row.RowType = DataControlRowType.DataRow Then
  Dim btn = CType(e.Row.FindControl("imgProDiagUploadRespuesta"), ImageButton)
  If btn IsNot Nothing Then
    ScriptManager.GetCurrent(Page).RegisterPostBackControl(btn)
  End If
End If

Make the server handler more robust (use NamingContainer rather than Parent.Parent, check for Nothing, sanitize names):

Protected Sub img_Click(sender As Object, e As ImageClickEventArgs)
  Dim img = CType(sender, ImageButton)
  Dim row = CType(img.NamingContainer, GridViewRow)
  Dim fup = CType(row.FindControl("fupvcProDiaRutaPropuesta"), FileUpload)
  If fup IsNot Nothing AndAlso fup.HasFile Then
    Dim safeName = IO.Path.GetFileName(fup.FileName)
    fup.SaveAs(Server.MapPath("~/Uploads/") & safeName)
  End If
End Sub

Quick checklist/troubleshooting:

  • Don’t re-bind the GridView on every postback (wrap DataBind inside If Not IsPostBack) — rebinding clears file inputs.
  • Temporarily remove UpdatePanel to confirm HasFile becomes true on a full postback.
  • If you need true AJAX uploads (progress, multiple files), use AsyncFileUpload/HTML5 FormData or a JS uploader library — those require different server code.
  • Sanitize file names, restrict types/sizes, and store files safely outside the web root where practical.

This keeps your onchange→click approach (as suggested by ) while ensuring the browser actually sends the file by forcing a full postback, which is what resolved the issue in the thread.

Recommended Answers

All 3 Replies

Write the following code in RowDataBound event of the GridView control. This code is to attach onChange event of the file upload control to the click event of image button control.

imgRespuesta = e.Row.FindControl("imgProDiagUploadRespuesta")fupRespuesta = e.Row.FindControl("fupvcProDiaRutaRespuesta")

fupRespuesta.Attributes.Add("onchange", "return document.getElementById('" + imgRespuesta.ClientID + "').click();")

Write this code in RowCreated event of the GridView. This code is to set style for image button and add handler to it.

imgRespuesta = e.Row.FindControl("imgProDiagUploadRespuesta")
imgRespuesta.Attributes.Add("Style", "Display:none")
AddHandler imgRespuesta.Click, AddressOf img_Click

I have tested this way in C#. It is working for me. I can able to reach img_Click event if a file is browsed and selected.

commented: Nice approach +2

I tried it, and it reaches the event, as you said, but the problem now is that the HasFile property of the FileUpload control is false when after I select the file... And I don't know why...

I hope you can help me with this...

Private Sub imgEnvio_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        Dim fup As FileUpload
        Dim img As ImageButton
        fup = CType(sender.Parent.Parent, GridViewRow).FindControl("fupvcProDiaRutaPropuesta")
        img = CType(sender.Parent.Parent, GridViewRow).FindControl("imgProDiagUploadPropuesta")
        If fup.HasFile Then <-- this is not true after I select the file
            img.Attributes.Remove("Style")
            Dim fullpath = IO.Path.GetFullPath(fup.FileName)
            Dim name = IO.Path.GetFileName(fullpath)
            fup.SaveAs(Server.MapPath("~") + "\Documentos\" & name.ToString)
        End If
    End Sub

Write the following code in RowDataBound event of the GridView control. This code is to attach onChange event of the file upload control to the click event of image button control.

imgRespuesta = e.Row.FindControl("imgProDiagUploadRespuesta")fupRespuesta = e.Row.FindControl("fupvcProDiaRutaRespuesta")

fupRespuesta.Attributes.Add("onchange", "return document.getElementById('" + imgRespuesta.ClientID + "').click();")

Write this code in RowCreated event of the GridView. This code is to set style for image button and add handler to it.

imgRespuesta = e.Row.FindControl("imgProDiagUploadRespuesta")
imgRespuesta.Attributes.Add("Style", "Display:none")
AddHandler imgRespuesta.Click, AddressOf img_Click

I have tested this way in C#. It is working for me. I can able to reach img_Click event if a file is browsed and selected.

Hi,

Using ajax, I manage to solve this, registering the imagebutton for postback in the scriptmanager... and that's it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.