Asp.net常用的操作函数[转]
时间:2011-05-23 来源:My fool
View Code
/// <summary>原文连接:http://www.cnblogs.com/powerfan/archive/2011/03/29/1998629.html
/// 取得本周第一天的日期,即星期日
/// </summary>
/// <returns></returns>
public static string GetWeekFirstDate()
{
return DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();
}
/// <summary>
/// 取得本周最后一天的日期
/// </summary>
/// <returns></returns>
public static string GetWeekLastDate()
{
return DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();
}
/// <summary>
/// 转换成Long型,错误返回-1
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static long ConvertToLong(object value)
{
long id = -1;
long.TryParse(value.ToString(), out id);
return id;
}
/// <summary>
/// 转换成Double型,错误返回-1
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static double ConvertToDouble(object value)
{
double id = -1;
double.TryParse(value.ToString(), out id);
return id;
}
/// <summary>
/// 转换成Int型,-1转换错误
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static int ConvertToInt(object value)
{
int id = -1;
int.TryParse(value.ToString(), out id);
return id;
}
/// <summary>
/// 绑定数据到ListControl
/// </summary>
/// <param name="lc"></param>
/// <param name="dt"></param>
/// <param name="strText"></param>
/// <param name="strValue"></param>
public static void BindDrop(ListControl lc, DataTable dt, string strText, string strValue)
{
lc.Items.Clear();
foreach (DataRow myRow in dt.Rows)
{
lc.Items.Add(new ListItem(myRow[strText].ToString(), myRow[strValue].ToString()));
}
lc.DataBind();
}
/// <summary>
/// 绑定数据到ListControl
/// </summary>
/// <param name="lc"></param>
/// <param name="dt"></param>
/// <param name="strText"></param>
/// <param name="strValue"></param>
public static void BindDrop(ListControl lc, DataTable dt, string strText, string strValue, string FirstText, string FirstValue)
{
lc.Items.Clear();
lc.Items.Add(new ListItem(FirstText, FirstValue));
foreach (DataRow myRow in dt.Rows)
{
lc.Items.Add(new ListItem(myRow[strText].ToString(), myRow[strValue].ToString()));
}
lc.DataBind();
}
/// <summary>
/// 绑定数据到ListControl
/// </summary>
/// <param name="lc"></param>
/// <param name="FirstText"></param>
/// <param name="FirstValue"></param>
public static void BindDrop(ListControl lc, string FirstText, string FirstValue)
{
lc.Items.Clear();
lc.Items.Add(new ListItem(FirstText, FirstValue));
lc.DataBind();
}
public static void BindDataDrop(ListControl list, int iBegin, int iEnd)
{
list.Items.Clear();
int count = iEnd - iBegin;
for (int i = iBegin; i <= iEnd; i++)
{
list.Items.Add(i.ToString());
}
}
/// <summary>
///根据指定值选定控件中中的项
/// </summary>
/// <param name="rbl">控件名</param>
/// <param name="flg">指定值</param>
/// <returns></returns>
public static bool SelectFlg(ListControl rbl, string flg)
{
bool isSelect = false;
int FlgCount = rbl.Items.Count;
if (flg == null)
{
flg = "";
}
if (flg != null)
{
for (int i = 0; i <= FlgCount - 1; i++)
{
if (rbl.Items[i].Value.Trim() == flg.Trim())
{
isSelect = true;
rbl.SelectedIndex = i;
break;
}
}
}
return isSelect;
}
/// <summary>
///根据指定值选定控件中中的项
/// </summary>
/// <param name="rbl">控件名</param>
/// <param name="flg">指定值</param>
/// <returns></returns>
public static void SelectFlg(ListControl rbl, DataTable dt, string ColumnName)
{
bool isSelect = false;
int FlgCount = rbl.Items.Count;
foreach (DataRow row in dt.Rows)
{
string flg = row[ColumnName].ToString();
if (flg == null)
{
flg = "";
}
if (flg != null)
{
for (int i = 0; i <= FlgCount - 1; i++)
{
if (rbl.Items[i].Value.Trim() == flg.Trim())
{
isSelect = true;
rbl.Items[i].Selected = true;
}
}
}
}
}
/// <summary>
///根据指定值选定控件中中的项
/// </summary>
/// <param name="rbl">控件名</param>
/// <param name="flg">指定值</param>
/// <returns></returns>
public static string[] GetDropSelect(ListControl rbl)
{
//rbl.Items
string strValue = "";
int FlgCount = rbl.Items.Count;
for (int i = 0; i <= FlgCount - 1; i++)
{
if (rbl.Items[i].Selected)
{
strValue += ";" + rbl.Items[i].Value;
//rbl.SelectedIndex = i;
}
}
strValue = strValue.Remove(0, 1);
return strValue.Split(';');
}
/// <summary>
///根据指定值选定控件中中的项
/// </summary>
/// <param name="rbl">控件名</param>
/// <param name="flg">指定值</param>
/// <returns></returns>
public static bool SelectFlg(ListControl rbl, object flg)
{
bool isSelect = false;
int FlgCount = rbl.Items.Count;
if (flg == null)
{
flg = "";
}
if (flg != null)
{
for (int i = 0; i <= FlgCount - 1; i++)
{
if (rbl.Items[i].Value.Trim() == flg.ToString().Trim())
{
isSelect = true;
rbl.SelectedIndex = i;
break;
}
}
}
return isSelect;
}
///<summary>
///根据指定值选定控件中中的项
/// </summary>
/// <param name="rbl">控件名</param>
/// <param name="flg">指定值</param>
/// <returns></returns>
public static bool SelectText(ListControl rbl, string flg)
{
bool isSelect = false;
int FlgCount = rbl.Items.Count;
if (flg == null)
{
flg = "";
}
if (flg != null)
{
for (int i = 0; i <= FlgCount - 1; i++)
{
if (rbl.Items[i].Text.Trim() == flg.Trim())
{
isSelect = true;
rbl.SelectedIndex = i;
break;
}
}
}
return isSelect;
}
/// <summary>
/// 将字符串转换为日期
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public static DateTime ParseDateTime(string p)
{
try
{
if (p != string.Empty)
{
return Convert.ToDateTime(p);
}
else
{
return DateTime.MinValue;
}
}
catch (Exception err)
{
return DateTime.Now;
}
}
/// <summary>
/// 将表中指定的一个字段构造成数组
/// </summary>
/// <param name="_SetUpedTable">Table</param>
/// <param name="column">指定的字段</param>
/// <returns></returns>
public static string[] AddTableColumnToArrayMethod(DataTable _SetUpedTable, string column)
{
string[] setUpArray = new string[_SetUpedTable.Rows.Count];
for (int i = 0; i < setUpArray.Length; i++)
{
setUpArray[i] = _SetUpedTable.Rows[i][column].ToString();
}
return setUpArray;
}
/// <summary>
/// 是否为日期型字符串
/// </summary>
/// <param name="StrSource">日期字符串(2008-05-08)</param>
/// <returns></returns>
public static bool IsDate(string StrSource)
{
return Regex.IsMatch(StrSource, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-9]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
}
/// <summary>
/// 是否为时间型字符串
/// </summary>
/// <param name="source">时间字符串(15:00:00)</param>
/// <returns></returns>
public static bool IsTime(string StrSource)
{
return Regex.IsMatch(StrSource, @"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
}
/// <summary>
/// 是否为日期+时间型字符串
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsDateTime(string StrSource)
{
return Regex.IsMatch(StrSource, @"^(((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$ ");
}
/// <summary>
/// 在指定的目录写入文件
/// </summary>
/// <param name="driectory">文件夹的路径</param>
/// <param name="content">写入的文本内容</param>
/// <param name="fileName">写入的文本文件名称</param>
public static void WriteFile(string driectory, string content, string fileName)
{
CreateDirectory(driectory);
FileStream fs = new FileStream(driectory + fileName, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine(content + "\n");
m_streamWriter.Flush();
m_streamWriter.Close();
fs.Close();
}
/// <summary>
/// 递归创建文件夹
/// </summary>
/// <param name="directoryName">文件夹的路径</param>
private static void CreateDirectory(string directoryName)
{
string sParentDirectory = Path.GetDirectoryName(directoryName);
if (!Directory.Exists(sParentDirectory))
CreateDirectory(sParentDirectory);
if (!Directory.Exists(directoryName))
Directory.CreateDirectory(directoryName);
}
相关阅读 更多 +