服务器端动态产生客户端脚本

来源:互联网 发布:怎么测网络是多少兆 编辑:程序博客网 时间:2024/04/30 18:43
    public static class CommonCode
    {
        public static void ShowMessage(System.Web.UI.Page page, string txt)
        {
            txt = txt.Replace("'", "").Replace("\r", "").Replace("\n", "");
            page.ClientScript.RegisterClientScriptBlock(page.GetType(), Guid.NewGuid().ToString(),
                "alert('" + txt + "');", true);
        }
     }




1.page为当前页面对象
2.RegisterClientScriptBlock第二参数为key,为了保证唯一性,让他总是生成,所以使用了Guid.NewGuid()方法
3.RegisterClientScriptBlock第四个参数为是否要生成Script标签