如何创建一个弹出消息框来显示.net c#web应用程序中的错误消息
本文关键字:消息 显示 错误 net c#web 应用程序 创建 何创建 一个 | 更新日期: 2023-09-27 17:57:27
有人知道我如何在服务器端创建一个弹出消息框,以便在保存过程失败时在弹出消息框中显示错误消息吗?
示例:
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
using (TransactionScope scope = new TransactionScope())
{
//save process
scope.Complete();
Response.Redirect(url);
}
}
catch (TransactionAbortedException ex)
{
//pop-up message box to show error message
}
catch (ApplicationException ex)
{
//pop-up message box to show error message
}
}
当保存过程失败时,我如何能够在catch中创建一个弹出消息框来向用户弹出错误消息框?
尝试使用Page.RegisterStartupScript
或ClientScript.RegisterStartupScript
方法。
ClientScript.RegisterStartupScript(
this.GetType(), "myalert", "alert('" + errorText + "');", true);
或
Response.Write(
@"<SCRIPT LANGUAGE=""JavaScript"">alert('" + errorText + "')</SCRIPT>");
这里有一种方法:
/// ---- ShowAlert --------------------------------
///
/// <summary>
/// popup a message box at the client
/// </summary>
/// <param name="page">A Page Object</param>
/// <param name="message">The Message to show</param>
public static void ShowAlert(Page page, String message)
{
String Output;
Output = String.Format("alert('{0}');",message);
page.ClientScript.RegisterStartupScript(page.GetType(), "Key", Output, true);
}
虽然提到的alert()脚本还可以,但它们给人的感觉相当业余。我推荐两种选择。
在你的页面上有一个标签,通常用红色和粗体字体隐藏。然后让它可见,并将文本设置为错误。
如果你想要一个对话框行为,可以使用每个人向你显示的内容,但不要弹出警报,而是从jQuery UI中选择一些不错的内容,其中包含一个不错的错误图标,甚至还有一个帮助链接或对话框中的其他内容。以下是一个示例:http://jqueryui.com/demos/dialog/#modal-消息
当然,如果你想让你的应用看起来更可靠,不要向他们显示任何错误。:)