开发者

ASP.NET Pass Error Messages

开发者 https://www.devze.com 2022-12-19 15:02 出处:网络
This is probably going to end up as a stupid question, but countless research has offered me no results.

This is probably going to end up as a stupid question, but countless research has offered me no results.

I know there are different types of errors I want to check for, and when I should be throwing an exception for "exceptional" errors, and that I should create validating functions for input and other checks.

My problem is, how do I send an error back to a page when the data entered fails in a separate class?

For Example:

  • User input entered in Page1.aspx, click calls Submit() in Class.vb
  • Class.vb finds that input is invalid
  • How do I update Page1.aspx label to say "Hey, that is not right".

I can do it on inline page, no problem, its passing it through 开发者_如何学Goa separate class that's causing me issues... Maybe I'm not even thinking of this correctly.

Any points in the right direction would be of huge help.

Thanks for the help in advance.


The simplest solution is to have Submit() return a boolean indicating whether there was an error or not:

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

It is a good practice to put your class in charge of its own errors, in which case you would expose an error message property:

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

The Submit function would look something like this:

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

精彩评论

暂无评论...
验证码 换一张
取 消