asp.net输出js到页面

来源:互联网 发布:linux 激活网卡 编辑:程序博客网 时间:2024/06/07 23:03

以上代码


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;    }}


0 0