以上代码
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
AlertMsg(Page, "保存失败,xx不能为空");
}
/// <summary>
/// 页面form表单最后注入脚本,通过alert提示msg信息
/// </summary>
/// <param name="pg">当前请求的页面对象</param>
/// <param name="msg">要提示的信息,会清除参数中的双引号、单引号、换行符</param>
public static void AlertMsg(System.Web.UI.Page pg, string msg)
{
StringBuilder sb = new StringBuilder();
if (msg != null)
{
msg = msg.Replace("\r\n", ";").Replace("\"", "").Replace("'", "");
}
if (msg.Length == 0)
{
return;
}
sb.Append("(function(){var m=\"");
sb.Append(msg);
sb.Append("\";alert(m);}());");
pg.ClientScript.RegisterStartupScript(pg.GetType(), DateTime.Now.Millisecond.ToString(), sb.ToString(), true);
sb = null;
}
}
本文介绍了一种在ASP.NET Web应用程序中实现自定义警告消息的方法。通过在页面加载事件中调用AlertMsg方法,可以在客户端弹出带有指定消息的警告框。此方法通过注册客户端脚本来实现,能够有效展示错误或提示信息。

1万+

被折叠的 条评论
为什么被折叠?



