共享两个做项目最常用功能操作的封装类
共享兩個做項目最常用功能操作的封裝類
一. 顯示消息對話框類
??? 做項目時總免不了要彈出對話框,或提示用戶,或做用戶確認。像WinForm下的MessageBox一樣很方便,但web下是不是也可以實現呢,答案是肯定的,我簡單總結了一下并封裝到類,在項目里像WinForm的MessageBox那樣直接調用就可以,也可以把它編譯成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;
?&,nbsp;???????????????? }
????????????? }
????????????? 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
}
轉載于:https://www.cnblogs.com/loway/archive/2006/01/16/318415.html
總結
以上是生活随笔為你收集整理的共享两个做项目最常用功能操作的封装类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 你敢抄底吗?内存价格开始跌了 DDR4首
- 下一篇: 黑莓“坐上”100万内最好轿跑!哪吒S官