namespace MRSOFTASPNET.CommonOperation
{
/// <summary>
/// 数据类型转换
/// </summary>
public class DataTypeConvert
{
#region 数据类型转换
public DataTypeConvert() { }
/// <summary>
/// 字符串转换为整数
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static int ConvertToInt(string value)
{ ///数据为空,返回-1
if (string.IsNullOrEmpty(value)) return -1;
int result = -1;
///执行转换操作
Int32.TryParse(value, out result);
return result;
}
/// <summary>
/// 字符串转换为时间
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static DateTime ConvertToDateTime(string value)
{ ///定义初始化值
DateTime result = DateTime.Parse("1900-01-01");
if (string.IsNullOrEmpty(value)) return result;
///执行转换操作
DateTime.TryParse(value, out result);
return result;
}
/// <summary>
/// 字符串转换为实数
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static decimal ConvertToDecimal(string value)
{ ///定义初始化值
decimal result = 0.0M;
if (string.IsNullOrEmpty(value)) return result;
///执行转换操作
decimal.TryParse(value, out result);
return result;
}
/// <summary>
/// 字符串转换为布尔类型
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool ConvertToBoolean(string value)
{ ///定义初始化值
bool result = false;
if (string.IsNullOrEmpty(value)) return result;
///执行转换操作
bool.TryParse(value, out result);
return result;
}
#endregion
}
public class DataBinder
{
#region 绑定控件的数据
public DataBinder() { }
/// <summary>
/// 绑定列表控件的数据,数据源为SqlDataReader对象
/// </summary>
/// <param name="list"></param>
/// <param name="dataSource"></param>
/// <param name="dataTextField"></param>
/// <param name="dataValueField"></param>
public static void BindListData(ListControl list, SqlDataReader dataSource,
string dataTextField, string dataValueField)
{
if (dataSource == null) { return; }
///绑定数据
list.DataSource = dataSource;
list.DataTextField = dataTextField;
list.DataValueField = dataValueField;
list.DataBind();
///关闭数据源
dataSource.Close();
}
/// <summary>
/// 绑定列表控件的数据,数据源为DataSet对象
/// </summary>
/// <param name="list"></param>
/// <param name="dataSource"></param>
/// <param name="dataTextField"></param>
/// <param name="dataValueField"></param>
public static void BindListData(ListControl list, DataSet dataSource,
string dataTextField, string dataValueField)
{
if (dataSource == null) { return; }
///绑定数据
list.DataSource = dataSource;
list.DataTextField = dataTextField;
list.DataValueField = dataValueField;
list.DataBind();
}
/// <summary>
/// 绑定列表控件的数据,数据源为DataTable对象
/// </summary>
/// <param name="list"></param>
/// <param name="dataSource"></param>
/// <param name="dataTextField"></param>
/// <param name="dataValueField"></param>
public static void BindListData(ListControl list, DataTable dataSource,
string dataTextField, string dataValueField)
{
if (dataSource == null) { return; }
///绑定数据
list.DataSource = dataSource;
list.DataTextField = dataTextField;
list.DataValueField = dataValueField;
list.DataBind();
}
/// <summary>
/// 绑定GridView控件的数据,数据源为SqlDataReader对象
/// </summary>
/// <param name="gv"></param>
/// <param name="dataSource"></param>
public static void BindGridViewData(GridView gv, SqlDataReader dataSource)
{
if (dataSource == null) { return; }
///绑定数据
gv.DataSource = dataSource;
gv.DataBind();
///关闭数据源
dataSource.Close();
}
/// <summary>
/// 绑定GridView控件的数据,数据源为DataSet对象
/// </summary>
/// <param name="gv"></param>
/// <param name="dataSource"></param>
public static void BindGridViewData(GridView gv, DataSet dataSource)
{
if (dataSource == null) { return; }
///绑定数据
gv.DataSource = dataSource;
gv.DataBind();
}
/// <summary>
/// 绑定GridView控件的数据,数据源为DataTable对象
/// </summary>
/// <param name="gv"></param>
/// <param name="dataSource"></param>
public static void BindGridViewData(GridView gv, DataTable dataSource)
{
if (dataSource == null) { return; }
///绑定数据
gv.DataSource = dataSource;
gv.DataBind();
}
/// <summary>
/// 绑定DataList控件的数据,数据源为SqlDataReader对象
/// </summary>
/// <param name="list"></param>
/// <param name="dataSource"></param>
public static void BindDataListData(DataList list, SqlDataReader dataSource)
{
if (dataSource == null) { return; }
///绑定数据
list.DataSource = dataSource;
list.DataBind();
///关闭数据源
dataSource.Close();
}
/// <summary>
/// 绑定DataList控件的数据,数据源为DataSet对象
/// </summary>
/// <param name="list"></param>
/// <param name="dataSource"></param>
public static void BindDataListData(DataList list, DataSet dataSource)
{
if (dataSource == null) { return; }
///绑定数据
list.DataSource = dataSource;
list.DataBind();
}
/// <summary>
/// 绑定DataList控件的数据,数据源为DataTable对象
/// </summary>
/// <param name="list"></param>
/// <param name="dataSource"></param>
public static void BindDataListData(DataList list, DataTable dataSource)
{
if (dataSource == null) { return; }
///绑定数据
list.DataSource = dataSource;
list.DataBind();
}
#endregion
}
public class ButtonEnable
{
#region 设置按钮的可用性
public ButtonEnable() { }
/// <summary>
/// 根据整数列表设置按钮的可用性
/// </summary>
/// <param name="button"></param>
/// <param name="idList"></param>
public static void ControlButtonEnable(Button button, int[] idList)
{ ///计算按钮的可用性
bool IsEnabled = true;
///列表为空
if (idList == null)
{
button.Enabled = IsEnabled;
return;
}
foreach (int id in idList)
{ ///如果id值大于0,则按钮可用
IsEnabled = IsEnabled && (id > 0 ? true : false);
if (IsEnabled == false) { break; }
}
button.Enabled = IsEnabled;
}
/// <summary>
/// 根据列表控件列表设置按钮的可用性
/// </summary>
/// <param name="button"></param>
/// <param name="listControl"></param>
public static void ControlButtonEnable(Button button,
params ListControl[] listControl)
{
bool IsEnabled = true;
///列表为空
if (listControl == null)
{
button.Enabled = IsEnabled;
return;
}
foreach (ListControl control in listControl)
{
if (control != null)
{ ///列表包含选择项,则按钮可用
IsEnabled = IsEnabled && (control.Items.Count > 0 ? true : false);
if (IsEnabled == false) { break; }
}
}
button.Enabled = IsEnabled;
}
/// <summary>
/// 根据整数列表和列表控件列表共同设置按钮的可用性
/// </summary>
/// <param name="button"></param>
/// <param name="idList"></param>
/// <param name="listControl"></param>
public static void ControlButtonEnable(Button button, int[] idList,
params ListControl[] listControl)
{ ///根据参数idList设置按钮可用性
ControlButtonEnable(button, idList);
///根据参数listControl设置按钮的可用性
ControlButtonEnable(button, listControl);
}
#endregion
}
public class ListSelectedItem
{
#region 设置列表控件的选择项
public ListSelectedItem() { }
/// <summary>
/// 根据Value属性把列表控件的选择项设置为指定项
/// </summary>
/// <param name="list"></param>
/// <param name="value"></param>
public static void ListSelectedItemByValue(ListControl list, string value)
{
if (list == null) return;
///选择项为空
if (list.Items.Count <= 0)
{
list.SelectedIndex = -1;
return;
}
///逐项进行比较,设置选择项
for (int i = 0; i < list.Items.Count; i++)
{
if (list.Items[i].Value == value)
{
list.SelectedIndex = i;
return;
}
}
///没有符合条件的选择项
list.SelectedIndex = -1;
}
/// <summary>
/// 根据Text属性把列表控件的选择项设置为指定项
/// </summary>
/// <param name="list"></param>
/// <param name="text"></param>
public static void ListSelectedItemByText(ListControl list, string text)
{
if (list == null) return;
///选择项为空
if (list.Items.Count <= 0)
{
list.SelectedIndex = -1;
return;
}
///逐项进行比较,设置选择项
for (int i = 0; i < list.Items.Count; i++)
{
if (list.Items[i].Text == text)
{
list.SelectedIndex = i;
return;
}
}
///没有符合条件的选择项
list.SelectedIndex = -1;
}
#endregion
}
public class DealwithString
{
#region 字符串处理
public DealwithString() { }
/// <summary>
/// 格式化时间为字符串
/// </summary>
/// <returns></returns>
public static string CreatedStringByTime()
{
return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
/// <summary>
/// 格式化超过指定长度的字符串,显示截取后字符串加...
/// </summary>
/// <param name="str">字符串</param>
/// <param name="displayLength">能显示的字节长度</param>
/// <returns></returns>
public static string FormatStringLength(string str, int displayLength)
{
//字符串默认编码的字节长度
int nameLenth = Encoding.Default.GetByteCount(str);
//字符串字节长度大于能显示的字节长度,进行截取
if (nameLenth > displayLength)
{
//减去将要附加到尾部的"..."的长度,得到要截取的字节长度
displayLength = displayLength - 3;
//当前遍历到的字节数,与displayLength比较退出循环
int CurrentLength = 0;
//要截取的Unicode(USC2)编码的字节长度,该长度不同于displayLength
int subLength = 0;
//字符串生成Unicode(USC2)编码的字节数组
byte[] strBytes = Encoding.Unicode.GetBytes(str);
for (; subLength < strBytes.GetLength(0) && CurrentLength < displayLength; subLength++)
{
if (subLength % 2 == 0)
{
CurrentLength++;
}
else
{
if (strBytes[subLength] > 0)
{
CurrentLength++;
}
}
}
if (subLength % 2 == 1)
{
if (strBytes[subLength] > 0)
{
subLength = subLength - 1;
}
else
{
subLength = subLength + 1;
}
}
return Encoding.Unicode.GetString(strBytes, 0, subLength) + "...";
}
else//长度未超限,不作格式化
{
return str;
}
}
#endregion
}
public class Dialog
{
public Dialog() { }
#region 显示对话框
/// <summary>
/// 弹出消息框
/// </summary>
/// <param name="response"></param>
/// <param name="message"></param>
public static void OpenDialog(HttpResponse response, string msg)
{
response.Write("<script>alert('" + msg + "')</script>");
}
/// <summary>
/// 弹出消息框并跳转
/// </summary>
/// <param name="response"></param>
/// <param name="message"></param>
public static void OpenDialog(HttpResponse response, string msg, string url)
{
response.Write("<script>alert('" + msg + "')</script>");
}
#endregion
}
}