2012-03-19 19 views
0

我想添加一個fileupload控件到我的aspx頁面,所以用戶可以添加圖片,但是當我在VB上執行代碼時,fileuploader控件無法識別。在Asp.net中的文件上傳錯誤VB

我有這個aspx頁面上的一個FormView內:

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

而且我有我的VB下面的代碼,但它說ErrorScreen沒有聲明。

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

這可能是一些非常愚蠢的事情,但我無法弄清楚是什麼問題。

請幫助。

乾杯

回答

2

由於您的FileUpload控件是InsertTemplate裏面,你不能直接訪問FileUpload控件。你必須做這樣的事情:

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 
+0

謝謝你的回答,解決了我的問題!乾杯 – CPM 2012-03-19 14:18:02