ASP.NET通过错误消息

问题描述:

这可能最终会成为一个愚蠢的问题,但无数研究都没有给我提供任何结果。ASP.NET通过错误消息

我知道存在不同类型的错误,我想检查哪些错误,以及何时应该为“特殊”错误抛出异常,并且应该为输入和其他检查创建验证函数。

我的问题是,当输入的数据在单独的类中失败时,如何将错误发送回页面?

例如:在Page1.aspx的进入

  • 用户输入,点击呼叫提交()在Class.vb
  • Class.vb发现输入无效
  • 如何更新第1页。 aspx标签说:“嘿,那是不正确的”。

我可以在内联页面上做到这一点,没问题,它通过一个单独的类传递给我的问题......也许我甚至没有正确地想到这一点。

在正确的方向上的任何点都将是巨大的帮助。

感谢您的帮助提前。

最简单的解决方案是提交()返回一个布尔值,指示是否有错误或不:

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