Why does the FileUpload always return false for the hasFile status?

I am using the FileUpload component but when I try to use it nothing happens.
I debugged and found that the hasFile returns false.

Here is the code I use. (Straight from MSDN)

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim FileUpload1 As New FileUpload
        If IsPostBack Then
            Dim path As String = Server.MapPath("~/UploadedImages/")
            Dim fileOK As Boolean = False

            If FileUpload1.HasFile Then
                Dim fileExtension As String
                fileExtension = System.IO.Path. _
                Dim allowedExtensions As String() = _
                    {".jpg", ".jpeg", ".png", ".gif"}
                For i As Integer = 0 To allowedExtensions.Length - 1
                    If fileExtension = allowedExtensions(i) Then
                        fileOK = True
                    End If
                If fileOK Then
                        FileUpload1.PostedFile.SaveAs(path & _
                        Label11.Text = "File uploaded!"
                    Catch ex As Exception
                        Label11.Text = "File could not be uploaded."
                    End Try
                    Label11.Text = "Cannot accept files of this type."
                End If
            End If
        End If
    End Sub
Who is Participating?
protenConnect With a Mentor Commented:
The first postback that occurs after the fileupload control contains data, is the only time the file uploads.  

If you set the upload control to a file location and then do anything at all to postback the page, the file will be there on the first postback only.  

So, if you are not going to save and/or process the file then, you need to save the file information in a session variable or some location that will persist on multiple postbacks.
so is the file uploading or not or have you answered your own question?
mackeyrjAuthor Commented:
Basicly you are right... I am using view state and the fileupload was in the wrong state so it was being posted before it was being filled with file location
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.