1

谁能告诉我如何在 C# 中的类库项目的 .cs 文件中显示警报消息?

我在类库项目中调用cs文件的方法。每当控件进入方法内的警报消息语句时,应在网页(aspx)上显示警报消息,并且不应执行警报消息语句的下一条语句。

4

3 回答 3

4

小心不要在类库中放置过多的 UI 代码。听起来您的类库方法应该引发异常。然后调用代码可以捕获异常并根据需要将其显示给用户。

于 2010-02-24T20:24:17.933 回答
2

In an application made of well-defined tiers, this should never be done.

There is a lack of detail in the question as to what you're trying to accomplish by doing this, but here are a few possibilities and solutions:

  • Using the alert as a substitute for debugging? Use the debugger and step through your code; inspect the variables as necessary.
  • Trying to capture the message of an exception that's getting thrown? Let the exception bubble up to the UI layer where you can catch it and show the message.
  • Trying to return a string from a database or from a localized lookup table? Return it as the result of a function or property, and show the value from the UI layer.
于 2010-02-24T20:38:18.993 回答
1

您的类库方法应该将错误返回给调用者,是否显示错误取决于调用者。

于 2010-02-24T20:37:07.597 回答