你的位置:首页 > ASP.net教程

[ASP.net教程]项目中常用类

1.分页数据模型:PagedDataModel.cs

用处:将分页的数据,封装到类中

public class PagedDataModel<T>{  /// <summary>  /// 总页数  /// </summary>  public int PageCount;  /// <summary>  /// 总行数  /// </summary>  public int RowCount;  /// <summary>  /// 当前页码  /// </summary>  public int PageIndex;  /// <summary>  /// 当前页码数据  /// </summary>  public IList<T> PagedList;}

2.数据帮助类:DataHelper.cs

用处:对数据进行处理,以一定格式返回

public static class DataHelper{  /// <summary>  /// js序列化器  /// </summary>  static JavaScriptSerializer jss=new JavaScriptSerializer();  /// <summary>  /// 将对象转换为json数组格式字符串   /// </summary>  /// <param name="obj"></param>  /// <returns></returns>  public static string ObjToJson(object obj)  {    return jss.Serialize(obj);  }  /// <summary>  /// 返回MD5加密字符串  /// </summary>  /// <param name="str"></param>  /// <returns></returns>  public static string MD5(string str)  {    return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, FormsAuthPasswordFormat.MD5.ToString());  }}

3.页面帮助类:PageHelper.cs

用处:向前台展示js信息时

public class PageHelper{  /// <summary>  /// 读取指定路径的文件,并返回字符串格式   /// </summary>  /// <param name="strPath">文件路径-物理路径 </param>  /// <returns></returns>  public static string ReadFile(string strPath)  {    return File.ReadAllText(strPath);  }  /// <summary>  /// 获取提示和跳转js代码字符串  /// </summary>  /// <param name="strMsg"></param>  /// <param name="strBackUrl"></param>  public static void WriteJSMsg(string strMsg, string strBackUrl)  {    string strBack = "<script>alert(" + strMsg + ");window.location='" + strBackUrl + "';</script>";    HttpContext.Current.Response.Write(strBack);  }  /// <summary>  /// 跳转js代码字符串  /// </summary>  /// <param name="strMsg"></param>  public static void WriteJSMsg(string strMsg)  {    string strBack = "<script>alert(" + strMsg + ");</script>";    HttpContext.Current.Response.Write(strBack);  }}

4.验证帮助类:ValidateHelper.cs

用处:对数据进行验证

public class ValidateHelper{  static Regex regIsNum=new Regex("^[0-9]+$");  /// <summary>  /// 验证 参数是否为整型数值  /// </summary>  /// <param name="strNum"></param>  /// <returns></returns>  public static bool IsNum(string strNum)  {    return regIsNum.IsMatch(strNum);  }}

5.异步消息处理类AjaxMsgHelper.cs

用处:将需要返回的信息,以一定的内容形式返回

public static class AjaxMsgHelper{  /// <summary>  /// 以json的格式返回消息  /// </summary>  /// <param name="statu">状态码</param>  /// <param name="msg">状态信息</param>  /// <param name="data">返回数据</param>  /// <param name="nextUrl">跳转url</param>  public static void AjaxMsg(string statu, string msg,string data,string nextUrl)  {    //{"statu":"err","msg":"出错啦","data":[{},{}],"nextUrl":"Login.aspx"}    string strMsg = "{\"statu\":\""+statu+"\",\"msg\":\""+msg+"\",\"data\":"+(data==null?"null":data)+",\"nextUrl\":\""+nextUrl+"\"}";    HttpContext.Current.Response.Write(strMsg);  }  public static void AjaxMsg(string statu, string msg, string data)  {    AjaxMsg(statu,msg,data,"null");  }  public static void AjaxMsg(string statu, string msg)  {    AjaxMsg(statu,msg,null,null);  }}