Upload File with Ajax does not work the first time it is initiated

I have an ASP.Net VB application with AJAX panels that has a bizarre behavior:

Client Side Code and Server Side code below.

The button is set to trigger a full postback (and does do that - even on the first time when it doesn't upload the file)

On the first run - server error returned on line ("imgContentType = MyFile.PostedFile.ContentType") is:
System.NullReferenceException: Object reference not set to an instance of an object.

All future times - works perfect...until you go to a different page and return.
<input id="MyFile" type="file" runat="Server" size="40" />
<input id="btnPictureUpload" type="submit" value="Upload" onserverclick="Upload_Click" runat="Server" />                                                                    <div id="UploadDetails" visible="False" runat="Server">
File Name: <span id="FileName" runat="Server" />
File Content: <span id="FileContent" runat="Server" />
File Size: <span id="FileSize" runat="Server" />bytes
Dim imgContentType As String
imgContentType = MyFile.PostedFile.ContentType

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I've used a slightly different solution.  This code runs in an update panel within a tab panel and works fine.

 <asp:UpdatePanel ID="upUpload" runat="server">
            <asp:PostBackTrigger ControlID="btnUpload" />
        <div class="TabPanel">
            <asp:Panel ID="pnlUpload" runat="server">
            <br />
            <asp:Label ID="Label2" runat="server" Text="Select an attachment to save. Document size is limited to 4MB."
            <br />
            <asp:FileUpload ID="fupAttachments" runat="server" Width="682px"  />
            <br />
            <br />
            <asp:Label ID="Label1" runat="server" Text="Description (Maximum 1000 characters.)"
            <br />
            <asp:TextBox ID="txtDesc" runat="server" Height="44px" MaxLength="1000" Width="674px"></asp:TextBox>
            <br />
            <br />
            <asp:Button ID="btnUpload" runat="server" Text="Upload" ToolTip="Click to save an attachment" />
            <br />
            <br />
            Additonal Instructions:<br />
            <br />
            Click the 'Browse' button. Select the file to upload, then click the 'Upload' button.<br />
Code Behind:
Public Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
        If (fupAttachments.HasFile) Then
                If (SaveFile(fupAttachments)) Then
                    ShowMessage("File Uploaded: " & fupAttachments.FileName & "; File Size: " & fupAttachments.PostedFile.ContentLength _
                        & "; File Type: " & fupAttachments.PostedFile.ContentType, False)
                    ShowMessage("The file could not be saved. Please report this to the system administrator.", True)
                End If
            Catch ex As Exception
                ShowMessage("The following error has occured: " & ex.ToString, True)
            End Try
            ShowMessage("You must specify a file to save.", True)
        End If
    End Sub

Open in new window

PapaLoraxAuthor Commented:
I am so frustrated - I can get your application to work. I can actually get a dummy setup to work for me too...but when I go back to my app, first time = "No file" all subsequent times...perfect.
PapaLoraxAuthor Commented:
It was because the Input Box was Visible=False at page load.

Once changed it was fine.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.