李天平的博客


天道酬勤
posts - 169, comments - 2976, trackbacks - 83, articles - 17
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

共享两个做项目最常用功能操作的封装类

Posted on 2005-12-03 01:32  李天平  阅读(...)  评论(...编辑  收藏

一. 显示消息对话框类

    做项目时总免不了要弹出对话框,或提示用户,或做用户确认。像WinForm下的MessageBox一样很方便,但web下是不是也可以实现呢,答案是肯定的,我简单总结了一下并封装到类,在项目里像WinFormMessageBox那样直接调用就可以,也可以把它编译成dll作为组件直接调用,并且它也没有弹出对话框时的大白屏现象:)。

     /// <summary>

     /// 显示消息提示对话框。

     /// 李天平

     /// 2005.10.1

     /// </summary>

     public class MessageBox

     {       

         private  MessageBox()

         {            

         }

         /// <summary>

         /// 显示消息提示对话框

         /// </summary>

         /// <param name="page">当前页面指针,一般为this</param>

         /// <param name="msg">提示信息</param>

         public static void  Show(System.Web.UI.Page page,string msg)

         {

              page.RegisterStartupScript("message","<script language='javascript' defer>alert('"+msg.ToString()+"');</script>");

         }

         /// <summary>

         /// 控件点击消息确认提示框

         /// </summary>

         /// <param name="page">当前页面指针,一般为this</param>

         /// <param name="msg">提示信息</param>

         public static void  ShowConfirm(System.Web.UI.WebControls.WebControl Control,string msg)

         {

              //Control.Attributes.Add("onClick","if (!window.confirm('"+msg+"')){return false;}");

              Control.Attributes.Add("onclick", "return confirm('" + msg + "');") ;

         }

         /// <summary>

         /// 显示消息提示对话框,并进行页面跳转

         /// </summary>

         /// <param name="page">当前页面指针,一般为this</param>

         /// <param name="msg">提示信息</param>

         /// <param name="url">跳转的目标URL</param>

         public static void ShowAndRedirect(System.Web.UI.Page page,string msg,string url)

         {

              StringBuilder Builder=new StringBuilder();

              Builder.Append("<script language='javascript' defer>");

              Builder.AppendFormat("alert('{0}');",msg);

              Builder.AppendFormat("top.location.href='{0}'",url);

              Builder.Append("</script>");

              page.RegisterStartupScript("message",Builder.ToString());

 

         }

         /// <summary>

         /// 输出自定义脚本信息

         /// </summary>

         /// <param name="page">当前页面指针,一般为this</param>

         /// <param name="script">输出脚本</param>

         public static void ResponseScript(System.Web.UI.Page page,string script)

         {

              page.RegisterStartupScript("message","<script language='javascript' defer>"+script+"</script>");

         }

     }

二.页面数据验证类

做项目,特别做MIS,更避免不了有用户输入数据需要做有效性验证,这里我总结封装了一个页面数据校验的工具类,用起来也是很方便。

     /// <summary>

     /// 页面数据校验类

     /// 李天平

     /// 2004.8

     /// </summary>

     public class PageValidate

     {

         private static Regex RegNumber = new Regex("^[0-9]+$");

         private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");

         private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");

         private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$

         private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样

         private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");

 

         public PageValidate()

         {

         }

         #region 数字字符串检查              

         /// <summary>

         /// 检查Request查询字符串的键值,是否是数字,最大长度限制

         /// </summary>

         /// <param name="req">Request</param>

         /// <param name="inputKey">Request的键值</param>

         /// <param name="maxLen">最大长度</param>

         /// <returns>返回Request查询字符串</returns>

         public static string FetchInputDigit(HttpRequest req, string inputKey, int maxLen)

         {

              string retVal = string.Empty;

              if(inputKey != null && inputKey != string.Empty)

              {

                   retVal = req.QueryString[inputKey];

                   if(null == retVal)

                       retVal = req.Form[inputKey];

                   if(null != retVal)

                   {

                       retVal = SqlText(retVal, maxLen);

                       if(!IsNumber(retVal))

                            retVal = string.Empty;

                   }

              }

              if(retVal == null)

                   retVal = string.Empty;

              return retVal;

         }       

         /// <summary>

         /// 是否数字字符串

         /// </summary>

         /// <param name="inputData">输入字符串</param>

         /// <returns></returns>

         public static bool IsNumber(string inputData)

         {

              Match m = RegNumber.Match(inputData);

              return m.Success;

         }       

         /// <summary>

         /// 是否数字字符串可带正负号

         /// </summary>

         /// <param name="inputData">输入字符串</param>

         /// <returns></returns>

         public static bool IsNumberSign(string inputData)

         {

              Match m = RegNumberSign.Match(inputData);

              return m.Success;

         }       

         /// <summary>

         /// 是否是浮点数

         /// </summary>

         /// <param name="inputData">输入字符串</param>

         /// <returns></returns>

         public static bool IsDecimal(string inputData)

         {

              Match m = RegDecimal.Match(inputData);

              return m.Success;

         }       

         /// <summary>

         /// 是否是浮点数可带正负号

         /// </summary>

         /// <param name="inputData">输入字符串</param>

         /// <returns></returns>

         public static bool IsDecimalSign(string inputData)

         {

              Match m = RegDecimalSign.Match(inputData);

              return m.Success;

         }

         #endregion

 

         #region 中文检测

         /// <summary>

         /// 检测是否有中文字符

         /// </summary>

         /// <param name="inputData"></param>

         /// <returns></returns>

         public static bool IsHasCHZN(string inputData)

         {

              Match m = RegCHZN.Match(inputData);

              return m.Success;

         }

         #endregion

 

         #region 邮件地址

         /// <summary>

         /// 是否是浮点数可带正负号

         /// </summary>

         /// <param name="inputData">输入字符串</param>

         /// <returns></returns>

         public static bool IsEmail(string inputData)

         {

              Match m = RegEmail.Match(inputData);

              return m.Success;

         }

         #endregion

 

         #region 其他

         /// <summary>

         /// 检查字符串最大长度,返回指定长度的串

         /// </summary>

         /// <param name="sqlInput">输入字符串</param>

         /// <param name="maxLength">最大长度</param>

         /// <returns></returns>         

         public static string SqlText(string sqlInput, int maxLength)

         {            

              if(sqlInput != null && sqlInput != string.Empty)

              {

                   sqlInput = sqlInput.Trim();                            

                   if(sqlInput.Length > maxLength)//按最大长度截取字符串

                       sqlInput = sqlInput.Substring(0, maxLength);

              }

              return sqlInput;

         }       

         /// <summary>

         /// 字符串编码

         /// </summary>

         /// <param name="inputData"></param>

         /// <returns></returns>

         public static string HtmlEncode(string inputData)

         {

              return HttpUtility.HtmlEncode(inputData);

         }

         /// <summary>

         /// 设置Label显示Encode的字符串

         /// </summary>

         /// <param name="lbl"></param>

         /// <param name="txtInput"></param>

         public static void SetLabel(Label lbl, string txtInput)

         {

              lbl.Text = HtmlEncode(txtInput);

         }

         public static void SetLabel(Label lbl, object inputObj)

         {

              SetLabel(lbl, inputObj.ToString());

         }       

 

         #endregion

}