ASP.NET一些公共方法commTools

时间:2023-03-09 21:55:28
ASP.NET一些公共方法commTools
 using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
/// <summary>
///commTools 的摘要说明
/// </summary>
namespace Intelligent.Common
{
public class commTools
{
public commTools()
{ }
//提示信息
public static void alert(TemplateControl target, string msg)
{
string scriptString = "alert('" + msg + "')";
target.Page.ClientScript.RegisterClientScriptBlock(typeof(TemplateControl), DateTime.Now.ToString().Replace(":", " "), scriptString, true);
}
//错误页面
public static void toErrorPage(string msg)
{
HttpContext.Current.Server.Transfer("~/Error.aspx?msg="+msg);
}
//获取对应参数的值,如果参数不合法则跳转到错误页面
public static string getQueryString(string key)
{
if (HttpContext.Current.Request.QueryString[key] != null)
{
string value = HttpContext.Current.Request.QueryString[key].ToString();
//可以根据key的不同,对值的类型和范围再进行判断和处理
return value;
}
else
toErrorPage("参数无效");
return null;
}
//根据某个值让RadioButtonList某项被选中
public static void setRadioButtonListByValue(RadioButtonList list, string value)
{
for (int i = ; i < list.Items.Count; i++)
{
if (list.Items[i].Value == value)
list.Items[i].Selected = true;
else
list.Items[i].Selected = false;
}
}
//根据某个值让DropDownList某项被选中
public static void setDropDownListByValue(DropDownList list, string value)
{
for (int i = ; i < list.Items.Count; i++)
{
if (list.Items[i].Value == value)
list.Items[i].Selected = true;
else
list.Items[i].Selected = false;
}
}
//根据传的集合,让CheckBoxList多项被选中
public static void SetCheckBoxListByList(CheckBoxList list,List<string> value)
{
for (int i = ; i < list.Items.Count; i++)
{
bool tag = false;
for (int j = ; j < value.Count; j++)
{
if (list.Items[i].Value == value[j])
{
tag = true;
break;
}
}
list.Items[i].Selected = tag;
}
}
//把一个字符串转换成对应的MD5
public static string toMD5(string str)
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5");
} private static Random m_rnd = new Random();
private static char getRandomChar()
{
int ret = m_rnd.Next();
while (ret < || (ret > && ret < ) || (ret > && ret < ))
{
ret = m_rnd.Next();
}
return (char)ret;
}
public static string getRandomString(int length)
{
StringBuilder sb = new StringBuilder(length);
for (int i = ; i < length; i++)
{
sb.Append(getRandomChar());
}
return sb.ToString();
} public static bool SendMail(string StrTo, string StrBody, string strSubject)
{
return false;
}
/// <summary>
/// 根据传进来的分割符,以及字符串将字符串分割
/// </summary>
/// <param name="str">需要分割的字符串</param>
/// <param name="spiltChar">分隔符</param>
/// <returns>返回分割好的字符串数组</returns>
public static string[] StringSpilt(string str, string spiltChar)
{
Regex regex = new Regex(spiltChar);
string[] strArr = regex.Split(str);
return strArr;
} /// <summary>
/// 去除字符串的末尾标志符
/// </summary>
/// <param name="str">字符串</param>
/// <param name="splitFlag">末尾标识符</param>
/// <returns>返回结果字符串</returns>
public static string RemoveLastSplitFlag(string str, string splitFlag)
{
int i = str.LastIndexOf(splitFlag);
if (i == -) //不存在末尾标志位
{
return str;
}
else
{
return str.Remove(i, splitFlag.Length);
} }