2010-02-03 87 views
4

這可能最終會成爲一個愚蠢的問題,但無數研究都沒有給我提供任何結果。ASP.NET通過錯誤消息

我知道存在不同類型的錯誤,我想檢查哪些錯誤,以及何時應該爲「特殊」錯誤拋出異常,並且應該爲輸入和其他檢查創建驗證函數。

我的問題是,當輸入的數據在單獨的類中失敗時,如何將錯誤發送回頁面?

例如:在Page1.aspx的進入

  • 用戶輸入,點擊呼叫提交()在Class.vb
  • Class.vb發現輸入無效
  • 如何更新第1頁。 aspx標籤說:「嘿,那是不正確的」。

我可以在內聯頁面上做到這一點,沒問題,它通過一個單獨的類傳遞給我的問題......也許我甚至沒有正確地想到這一點。

在正確的方向上的任何點都將是巨大的幫助。

感謝您的幫助提前。

回答

2

最簡單的解決方案是提交()返回一個布爾值,指示是否有錯誤或不:

If class.Submit() = False Then 
    lblError.Text = "Hey, that is not right." 
End If 

這是把你的班級負責自己的錯誤的一個很好的做法,其中情況下,你會暴露的錯誤信息屬性:

If class.Submit() = False Then 
    lblError.Text = class.GetErrorMessage() 
End If 

提交操作會是這個樣子:

Public Function Submit() As Boolean 
    Dim success As Boolean = False 
    Try 
     ' Do processing here. Depending on what you do, you can 
     ' set success to True or False and set the ErrorMessage property to 
     ' the correct string. 
    Catch ex As Exception 
     ' Check for specific exceptions that indicate an error. In those 
     ' cases, set success to False. Otherwise, rethrow the error and let 
     ' a higher up error handler deal with it. 
    End Try 

    Return success 
End Function 
+0

謝謝,我知道這是件容易的事。我最終這樣做了,並且能夠創建一個錯誤屬性來傳遞任何自定義消息,如果success = false。 我不是看着它正確的方式,再次感謝! – JBickford 2010-02-04 14:30:01