JavaScript 几个常用的弹出对话框提示案例

在Asp.net 2.0 开发过程中,常会需要用到弹出对话框提示,我做了简单的封装,使用方便,贴出来。

    public class Jscript
    {
        private static string s1 = "<script language='JavaScript'>";
        private static string s2 = "</script>";

        private static string Quote(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return str;
            }
            else
            {
                return str.Replace("'", "\\'");
            }
        }

        private static void Write(string str)
        {
            HttpContext.Current.Response.Write(str);
        }

        /// <summary>
        /// 提示消息
        /// </summary>
        /// <param name="strMsg"></param>
        public static void alert(string strMsg)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(s1);
            sb.AppendFormat("alert('{0}');", Quote(strMsg));
            sb.Append(s2);
            Write(sb.ToString());
        }

        /// <summary>
        /// 提示消息并返回
        /// </summary>
        /// <param name="strMsg"></param>
        public static void alertAndBack(string strMsg)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(s1);
            sb.AppendFormat("alert('{0}');history.go(-1)", Quote(strMsg));
            sb.Append(s2);
            Write(sb.ToString());
        }

        /// <summary>
        /// 提示消息并跳转
        /// </summary>
        /// <param name="strMsg"></param>
        public static void AlertAndGoto(string strMsg, string strUrl)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(s1);
            sb.AppendFormat("alert('{0}');location.href='{1}';", Quote(strMsg), strUrl);
            sb.Append(s2);
            Write(sb.ToString());
        }

        /// <summary>
        /// 提示消息并关闭(刷新父窗体)
        /// </summary>
        /// <param name="strMsg"></param>
        public static void alertAndClose(string strMsg)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(s1);
            sb.AppendFormat("alert('{0}');window.opener.location.reload();window.close();", Quote(strMsg));
            sb.Append(s2);
            Write(sb.ToString());
        }

        /// <summary>
        /// 提示消息并关闭
        /// </summary>
        /// <param name="strMsg"></param>
        public static void alertAndClosePage(string strMsg)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(s1);
            sb.AppendFormat("alert('{0}');window.close();", Quote(strMsg));
            sb.Append(s2);
            Write(sb.ToString());
        }

        /// <summary>
        /// 提示消息并打开新的
        /// </summary>
        /// <param name="strMsg"></param>
        public static void alertAndOpen(string strMsg, string strUrl)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(s1);
            sb.AppendFormat("alert('{0}');window.open('{1}');window.close();", Quote(strMsg), Quote(strUrl));
            sb.Append(s2);
            Write(sb.ToString());
        }

        // <summary>
        /// 提示消息并跳转,关闭自窗体
        /// </summary>
        /// <param name="strMsg"></param>
        public static void AlertAndGotoAndClose(string strMsg, string strUrl)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(s1);
            sb.AppendFormat("alert('{0}');window.parent.opener.location.href='{1}';window.close();", Quote(strMsg), strUrl);
            sb.Append(s2);
            Write(sb.ToString());
        }
    }


 

你可能感兴趣的:(JavaScript,String,Class,asp.net)