/// /// 获取、绑定、校验控件值的操作方法类 /// public class ControlHelper { /// /// 文本框不能为空值 /// /// 文件框控件ID /// 文本框控件名称 /// public static string GetErrorMessage(TextBox txt, string txtName) { if(txt.Text.Trim() == string.Empty) return string.Format("
{0}不能为空值!", txtName); else return string.Empty; } ///
/// 文本框不能为空值且不能超过允许字符 /// ///
文本框控件ID ///
文本框控件名称 ///
最大长度 ///
public static string GetErrorMessage(TextBox txt, string txtName, int maxLength) { if(txt.Text.Trim() == string.Empty || txt.Text.Trim().Length > maxLength) return string.Format("
{0}不能为空值且不能超过{1}个字符!", txtName, maxLength); else return string.Empty; } ///
/// 文本控件值校验类型 /// public enum TextValueType { ///
/// int 类型,包括负数 /// Int, ///
/// 小数类型 /// Decimal, ///
/// 整形数字类型 /// Numeric } ///
/// 检查文本框必须为数字 /// ///
文本框控件ID ///
文本框控件名称 ///
是否为Int数字值,否则为小数值 ///
是否必须输入 ///
public static string GetErrorMessage(TextBox txt, string txtName, TextValueType tvt, bool isRequire) { if(isRequire && txt.Text.Trim() == string.Empty) return string.Format("
{0}不能为空值!", txtName); switch(tvt) { case TextValueType.Int: if(!Validator.IsInt(txt.Text.Trim()) && txt.Text.Trim() != string.Empty) return string.Format("
{0}的值必须为Int型数字!", txtName); break; case TextValueType.Decimal: if(!Validator.IsDecimal(txt.Text.Trim()) && txt.Text.Trim() != string.Empty) return string.Format("
{0}的值必须为数字或小数且不能为负数!", txtName); break; case TextValueType.Numeric: if(!Validator.IsNumeric(txt.Text.Trim()) && txt.Text.Trim() != string.Empty) return string.Format("
{0}的值必须为整型数字!", txtName); break; } return string.Empty; } ///
/// 检验下拉框必须选择索引大于第一项的值 /// ///
///
///
public static string GetErrorMessage(DropDownList ddl, string ddlName) { if(ddl.SelectedIndex <= 0) return string.Format("
请选择{0}!", ddlName); else return string.Empty; } ///
/// 检验多项单选框必须选择 /// ///
///
///
public static string GetErrorMessage(RadioButtonList rbl, string rblName) { if(rbl.SelectedIndex == -1) return string.Format("
请选择{0}!", rblName); else return string.Empty; } ///
/// 只允许上传图片 /// ///
///
///
public static string GetErrorMessage(FileUpload upload, string fuName) { if(upload.HasFile) { if(upload.PostedFile.ContentType.ToString().ToLower().IndexOf("image") == -1) return string.Format("
上传的{0}文件格式不正确!", fuName); } return string.Empty; } ///
/// 获得文本框的数字值 /// ///
文本框控件 ///
public static int GetIntValue(TextBox txt) { if(txt.Text.Trim() == string.Empty) return 0; try { return Convert.ToInt32(txt.Text.Trim()); } catch { return 0; } } ///
/// 获得下拉控件选择项索引大于0的Int值 /// ///
///
public static int GetIntValue(DropDownList ddl) { if(ddl.SelectedIndex > 0) return Convert.ToInt32(ddl.SelectedValue); else return 0; } ///
/// 获得文本框的小数值 /// ///
文本框控件 ///
是否为小数 ///
public static decimal GetDecimalValue(TextBox txt) { if(txt.Text.Trim() == string.Empty) return 0; try { return Convert.ToDecimal(txt.Text.Trim()); } catch { return 0; } } ///
/// 返回从CheckBoxList中选定的单个或多个值 /// ///
CheckBoxList控件名称 ///
返回选定的值 public static string GetValue(CheckBoxList cbl) { string result = string.Empty; for(int i = 0; i < cbl.Items.Count; i++) { if(cbl.Items[i].Selected == true) result += String.Format(",{0}", cbl.Items[i].Value); } if(result != string.Empty) result = result.Substring(1, result.Length - 1); return result; } ///
/// 从CheckBoxList中选定 /// ///
CheckBoxList控件名称 ///
要选中项值,多个值以,号间隔 public static void BindValue(CheckBoxList cbl, string val) { Array.ForEach(val.Split(','), delegate(string s) { for(int i = 0; i < cbl.Items.Count; i++) if(cbl.Items[i].Value == s) cbl.Items[i].Selected = true; }); } ///
/// 从单选项中选定 /// ///
RadioButtonList控件名称 ///
要选定的值 public static void BindValue(RadioButtonList rbl, string val) { for(int i = 0; i < rbl.Items.Count; i++) { if(rbl.Items[i].Value == val) rbl.Items[i].Selected = true; } } }