簡體   English   中英

ASP.NET傳遞錯誤消息

[英]ASP.NET Pass Error Messages

這可能最終會成為一個愚蠢的問題,但是無數的研究沒有給我任何結果。

我知道我要檢查的錯誤類型不同,何時應該為“異常”錯誤引發異常,並且應該為輸入和其他檢查創建驗證函數。

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

例如:

  • 在Page1.aspx中輸入用戶輸入,單擊在Class.vb中調用Submit()
  • Class.vb發現輸入無效
  • 如何將Page1.aspx標簽更新為“嘿,那不對”。

我可以在內聯頁面上完成它,沒問題,將它傳遞給一個單獨的類,這會引起我的問​​題……也許我什至沒有正確地考慮這一點。

朝正確方向的任何觀點都會有很大幫助。

我在這里先向您的幫助表示感謝。

最簡單的解決方案是讓Submit()返回一個布爾值,該布爾值指示是否存在錯誤:

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

Submit函數將如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM