ASP.NET一些公共方法commTools

  1 using System;

  2 using System.Data;

  3 using System.Configuration;

  4 using System.Web;

  5 using System.Web.Security;

  6 using System.Web.UI;

  7 using System.Web.UI.HtmlControls;

  8 using System.Web.UI.WebControls;

  9 using System.Web.UI.WebControls.WebParts;

 10 using System.Collections;

 11 using System.Security.Cryptography;

 12 using System.Text;

 13 using System.IO;

 14 using System.Collections.Generic;

 15 using System.Text.RegularExpressions;

 16 /// <summary>

 17 ///commTools 的摘要说明

 18 /// </summary>

 19 namespace Intelligent.Common

 20 {

 21     public class commTools

 22     {

 23         public commTools()

 24         {

 25 

 26         }

 27         //提示信息

 28         public static void alert(TemplateControl target, string msg)

 29         {

 30             string scriptString = "alert('" + msg + "')";

 31             target.Page.ClientScript.RegisterClientScriptBlock(typeof(TemplateControl), DateTime.Now.ToString().Replace(":", " "), scriptString, true);

 32         }

 33         //错误页面

 34         public static void toErrorPage(string msg)

 35         {

 36             HttpContext.Current.Server.Transfer("~/Error.aspx?msg="+msg);

 37         }

 38         //获取对应参数的值,如果参数不合法则跳转到错误页面

 39         public static string getQueryString(string key)

 40         {

 41             if (HttpContext.Current.Request.QueryString[key] != null)

 42             {

 43                 string value = HttpContext.Current.Request.QueryString[key].ToString();

 44                 //可以根据key的不同,对值的类型和范围再进行判断和处理

 45                 return value;

 46             }

 47             else

 48                 toErrorPage("参数无效");

 49             return null;

 50         }

 51         //根据某个值让RadioButtonList某项被选中

 52         public static void setRadioButtonListByValue(RadioButtonList list, string value)

 53         {   

 54             for (int i = 0; i < list.Items.Count; i++)

 55             {

 56                 if (list.Items[i].Value == value)

 57                     list.Items[i].Selected = true;

 58                 else

 59                     list.Items[i].Selected = false;

 60             }

 61         }

 62         //根据某个值让DropDownList某项被选中

 63         public static void setDropDownListByValue(DropDownList list, string value)

 64         {

 65             for (int i = 0; i < list.Items.Count; i++)

 66             {

 67                 if (list.Items[i].Value == value)

 68                     list.Items[i].Selected = true;

 69                 else

 70                     list.Items[i].Selected = false;

 71             }

 72         }

 73         //根据传的集合,让CheckBoxList多项被选中

 74         public static void SetCheckBoxListByList(CheckBoxList list,List<string> value)

 75         {

 76             for (int i = 0; i < list.Items.Count; i++)

 77             {

 78                 bool tag = false;

 79                 for (int j = 0; j < value.Count; j++)

 80                 {

 81                     if (list.Items[i].Value == value[j])

 82                     {

 83                         tag = true;

 84                         break;

 85                     }

 86                 }

 87                 list.Items[i].Selected = tag;

 88             }

 89         }

 90         //把一个字符串转换成对应的MD5

 91         public static string toMD5(string str)

 92         {

 93             return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5");

 94         }

 95        

 96         private static Random m_rnd = new Random();

 97         private static char getRandomChar()

 98         {

 99             int ret = m_rnd.Next(122);

100             while (ret < 48 || (ret > 57 && ret < 65) || (ret > 90 && ret < 97))

101             {

102                 ret = m_rnd.Next(122);

103             }

104             return (char)ret;

105         }

106         public static string getRandomString(int length)

107         {

108             StringBuilder sb = new StringBuilder(length);

109             for (int i = 0; i < length; i++)

110             {

111                 sb.Append(getRandomChar());

112             }

113             return sb.ToString();

114         }

115 

116         public static bool SendMail(string StrTo, string StrBody, string strSubject)

117         {

118             return false;

119         }

120         /// <summary>

121         /// 根据传进来的分割符,以及字符串将字符串分割

122         /// </summary>

123         /// <param name="str">需要分割的字符串</param>

124         /// <param name="spiltChar">分隔符</param>

125         /// <returns>返回分割好的字符串数组</returns>

126         public static string[] StringSpilt(string str, string spiltChar)

127         {

128             Regex regex = new Regex(spiltChar);

129             string[] strArr = regex.Split(str);

130             return strArr;

131         }

132 

133         /// <summary>

134         /// 去除字符串的末尾标志符

135         /// </summary>

136         /// <param name="str">字符串</param>

137         /// <param name="splitFlag">末尾标识符</param>

138         /// <returns>返回结果字符串</returns>

139         public static string RemoveLastSplitFlag(string str, string splitFlag)

140         {

141             int i = str.LastIndexOf(splitFlag);

142             if (i == -1)    //不存在末尾标志位

143             {

144                 return str;

145             }

146             else

147             {

148                 return str.Remove(i, splitFlag.Length);

149             }

150 

151         }

 

你可能感兴趣的:(asp.net)