如何在 ASP.NET 中显示错误消息?

发布于 2024-08-05 13:38:41 字数 238 浏览 5 评论 0原文

我有一堆文本框和一个保存按钮来更新某些内容。当我单击“保存”时,代码隐藏文件中的代码可以确定它们是否正确填写。

如果未正确填写,我想以警报的形式显示错误消息。

最好的方法是什么?按下按钮显然会使页面回发,所以我考虑在 URL 中添加一些内容,例如 mypage.aspx?errormessage=blahblah 但我不知道这是否是最好的方法,甚至不知道该怎么做它...

有什么建议吗?

I have a bunch of text boxes and a save button to update something. When I click Save I have code that determines whether they are correctly filled in, in the code behind file.

If they are not correctly filled in, I want to display an error message in the form of an alert.

What is the best way to do this? Pressing the button obviously makes the page postback, so I thought about adding something to the URL like mypage.aspx?errormessage=blahblah but I don't know if this is the best way or even how to do it...

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

骄兵必败 2024-08-12 13:38:42

首先,我不建议向用户显示模式警报框。

您可以从服务器端代码调用 JavaScript 函数,并在该函数中弹出错误。

或者您可以发出 AJAX 请求,在服务器端验证后,您可以将响应发送回客户端。

First of all I won't recommend showing an modal alert box to the user.

You can call a javascript function from the server side code and in that function you can pop out the error.

Or you can issue an AJAX request and after the validation on server side you can send back a response to the client.

拔了角的鹿 2024-08-12 13:38:42

ASP.NET 的各种验证控件(启用客户端验证)加上适当的错误消息和/或摘要消息对于大多数情况来说已经足够了。

对于“AJAX 感觉和行为”,将控件放入更新面板也将很容易实现。

ASP.NET's various validation controls (with client-side validation enabled), coupled with proper error messages and/or summary message will be good enough for most scenarios.

For 'AJAX feel and behaviour', put the controls into an updatepanel will be easy to implement too.

怕倦 2024-08-12 13:38:42

尽可能多地使用旧的验证器,它们向客户端呈现一些 javascript,以便您可以使用这些控件进行大量验证,并且只有当它们都满意时才允许页面提交。

它们确实会在每次提交时触发,因此如果您不希望每个提交操作都触发它们,您可以将控件作为验证组的一部分。

他们可以使用正则表达式验证输入,确保某个字段有一个值,并且还有更多值。

然后是声明“消息”的属性和显示所有验证器消息的控件。一切都非常漂亮,并且直接内置在 IDE 中。

去检查验证器控件。

Use ye olde Validators as much as poss, they render out some javascript to the client so yu can do alot of validation using these controls and only when they are all satisfied does it allow the page to submit.

They do fire on every submit so if you don't want every submit action to fire them you make the controls part of a validation group.

They can validate input using regular expressions, make sure a field has a value and there is a few more as well.

Then there is property to declare a 'message' and a control to show all the validator messages. All very swish and built right into the IDE.

Go check out the validator controls.

不必了 2024-08-12 13:38:42

在按钮单击事件中尝试以下代码:

string strErr="Error!";//Put your error message here
ClientScript.RegisterStartupScript(GetType(), "scrptName", "javascript: alert('"+strErr+"'); ", true);

它将显示一条警报消息。

否则,在您的 aspx 页面上放置一个标签,并在 page_load 事件中设置visible false。
当按钮事件中发生错误时,将标签可见性设置为“true”并使用错误消息填充标签文本。

Try the following code in your button click event:

string strErr="Error!";//Put your error message here
ClientScript.RegisterStartupScript(GetType(), "scrptName", "javascript: alert('"+strErr+"'); ", true);

It will show an alert message.

Else put a label on your aspx page and set visible false in page_load event.
When error occurs in your button event set the label visibility 'true' and fill the label text with the error message.

青柠芒果 2024-08-12 13:38:41

模态警报很糟糕,回发也是如此。尝试在客户端尽可能多地进行检查,而无需往返服务器。请参阅 jQuery 验证 插件以获得侵入性较小的验证方式。

Modal alerts are bad, as are postbacks. Try to check as much as possible on the client-side without a round-trip to server. See jQuery Validation plugins for a less intrusive way of validation.

放飞的风筝 2024-08-12 13:38:41

您可以使用 CustomValidator 触发显示警报框的客户端脚本吗?

Could you use a CustomValidator to trigger client side script that shows a alert box?

智商已欠费 2024-08-12 13:38:41

您可以在服务器端错误处理代码中使用 ClientScript.RegisterStartupScript() 方法来编写调用alert('message') 的javascript 片段,如下所示

private void ShowErrorMessage(string message)
{
    string script = "alert('" + message + "');";
    ClientScript.RegisterStartupScript(typeof(MyPage), "errorScript", script, true);
}

但我建议您改用验证器。如果您实现自己的自定义验证器,则可以使其发出可以在提交之前运行的客户端脚本,以避免完全回发。

验证器的一个好处是它们的错误消息可以显示在页面上的 ValidatorSummary 中,避免出现难看的警报框。

You could use the ClientScript.RegisterStartupScript() method in the server side error handling code to write a javascript snippet which calls alert('message'), something like this

private void ShowErrorMessage(string message)
{
    string script = "alert('" + message + "');";
    ClientScript.RegisterStartupScript(typeof(MyPage), "errorScript", script, true);
}

But I would recommend you use a validator instead. If you implement your own custom validator, you can make it emit client-side script which can run before the submit, to avoid the postback altogether.

A good thing about validators is that their error messages can be displayed in a ValidatorSummary on the page, avoiding the ugly alert box.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文