public class Alert
    {

        #region 警告函数
        ///<summary>
        /// 写警告,仅弹出对话框
        ///</summary>
        public static void MessageBox(string text)//弹出对话框
        {
            HttpContext.Current.Response.Write("<Script language=javascript>alert('" + text + "');</script>");
        }
        ///<summary>
        /// 写警告,仅弹出对话框关闭窗口
        ///</summary>
        public static void MessageBoxClose(string text)//弹出对话框
        {
            HttpContext.Current.Response.Write("<Script language=javascript>");
            HttpContext.Current.Response.Write("alert('" + text + "');");
            HttpContext.Current.Response.Write("window.close()");
            HttpContext.Current.Response.Write("</script>");
        }

        ///<summary>
        /// 写警告,返回原来页面
        ///</summary>
        public static void MessageBoxBack(string text)//返回原来页面
        {
            HttpContext.Current.Response.Write("<Script language=javascript>");
            HttpContext.Current.Response.Write("alert('" + text + "');");
            HttpContext.Current.Response.Write("window.history.go(-1)");
            HttpContext.Current.Response.Write("</script>");
        }
        ///<summary>
        /// 页面转向
        /// text 警告文本,转向页面,转向目标
        ///</summary>
        public static void MessageBoxRedirect(string text, string gourl, string target)
        {
            if (target == "" || target == null)
            { target = "window"; }
            HttpContext.Current.Response.Write("<Script language=javascript>");
            HttpContext.Current.Response.Write("alert('" + text + "');");
            HttpContext.Current.Response.Write("window." + target + ".location='" + gourl + "';");
            HttpContext.Current.Response.Write("</script>");
        }
        ///<summary>
        /// 页面转向
        /// text 警告文本,转向页面
        ///</summary>
        public static void MessageBoxRedirect(string text, string gourl)
        {
            HttpContext.Current.Response.Write("<Script language=javascript>");
            HttpContext.Current.Response.Write("alert('" + text + "');");
            HttpContext.Current.Response.Write("window.location='" + gourl + "';");
            HttpContext.Current.Response.Write("</script>");
        }
        #endregion


        /// <summary>
        /// 处理用户输入的危险代码
        /// </summary>
        ///定义InputText函数处理用于输入
        public static string InputText(string inputString, int maxLength)
        {
            StringBuilder retVal = new StringBuilder();     ///构造临时字符串数组
            if ((inputString != null) && (inputString != String.Empty))
            {
                inputString = inputString.Trim();         ///清空字符串两段的空白符号
                if (inputString.Length > maxLength)
                {   ///设置字符串的长度
                    inputString = inputString.Substring(0, maxLength);
                }
                for (int i = 0; i < inputString.Length; i++)
                {
                    switch (inputString[i])
                    {
                        ///去除危险字符串(\ ,/,:,*,?,",<,>,| )
                        case '*':
                        case '|':
                        case '<':
                        case '>':
                        case '/':
                        case '\\':
                        case ':':
                        case '?':
                        case '"':
                            retVal.Append(""); break;
                        default: retVal.Append(inputString[i]); break;
                    }
                }
                retVal.Replace("'", " ");
            }
            return retVal.ToString();
        }

 /// <summary>
        /// 动态加载下拉菜单
        /// </summary>
        public static void AddListItem(ListControl control, string value, string text)
        {
            ListItem item = new ListItem();
            item.Text = text;
            item.Value = value;
            control.Items.Add(item);
        }

posted on 2007-07-17 14:29  萧佰刚  阅读(234)  评论(0编辑  收藏  举报