我正在创建一个网站应用程序(ASP.NET, C#),想知道显示错误/警告消息的最佳方法。是通过MessageBox还是Label?只需要一些建议。
我正在创建一个网站应用程序(ASP.NET, C#),想知道显示错误/警告消息的最佳方法。是通过MessageBox还是Label?只需要一些建议。
我更倾向于以这种方式来实现:
this.RegisterClientScriptBlock(typeof(string), "key", string.Format("alert('{0}');", ex.Message), true);
对于服务器端验证,您可以编写一个自定义控件(我已经完成了),如下所示,以便在整个站点中一致地显示消息。
对于客户端验证,您可以使用验证摘要。
<asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="List"
ShowMessageBox="true" ShowSummary="false" />
<label>First name<br>
<input type="text" name="firstname" required></label>
<span class="error">You must enter a first name</span>
你的问题对我来说有点不清楚。
无论如何,如果我们谈论一般的验证:
你可以使用 JavaScript
或 jQuery
来进行客户端验证,这是现今广泛使用的方法。
或者你可以使用 asp.net
验证控件,例如 RequiredFieldValidator。
此外,请阅读关于构建更好的 Web 表单:验证最佳实践和在 ASP.NET 中使用 Enterprise Library 验证应用程序块的文章。
There are many ways you can display the error message.
1)Simple Message Box.You may need add System.Windows as namespace in application.
2)The same message box look and feel you can create using the below code,
this.RegisterClientScriptBlock(typeof(string), "key", string.Format("alert('{0}');", ex.Message), true);
3)Using InBuilt Asp.net validation control like Required Field Validator,RangeValidator,Validatio Summary.
4)Place a Label control on each page,use it as when required to display error msg.
5)Define a validation class,create rules and error message commonly used across apllications.
6)Using Javascript prompt and alert also,you can display it.
有很多使用谷歌的网站可以给出这个主题的良好概述。这里是一个很好的例子。