0

I am trying to add a fileupload control to my aspx page so the user can add pictures, but when I am implementing the code behind on VB the fileuploader controler is not recognized.

I have this on aspx page inside a formview:

<InsertItemTemplate>   
    <div id="TaskScreenError">
        Upload a Screenshot of Error:
        <asp:FileUpload ID="ErrorScreen" runat="server"   />
    </div>
<InsertItemTemplate>

And I have the following code on my VB, but it says ErrorScreen is not declared.

Dim filereceived As String = ErrorScreen.PostedFile.FileName  
    ' validate the file to ensure it is an image
    Select Case Right(filereceived, 4)
        Case ".jpg", ".tif", ".bmp", ".gif"
        Case Else
            lblErrMsg.Text = "Image is in a format we don't accept, please use jpg, tif, bmp or gif."
            Exit Sub
    End Select
    ...

It might be something really stupid but I cant figure it out what is the problem.

Please help.

Cheers

1 Answer 1

2

Since your FileUpload control is inside the InsertTemplate, you cannot access the FileUpload control directly. You have to do something like this:

Dim fileUpload As FileUpload = TryCast(YOURFORMVIEWID.FindControl("ErrorScreen"), FileUpload)
If fileUpload Is Nothing Then    
    ' Handle if the FileUpload can't be found
Else    
    Dim filereceived = fileUpload.PostedFile.FileName    
    ' Continue your code here...
End If
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.