·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> BS常用方法备忘
在B/S项目开发过程中总结的一些常用方法,如:常量、验证方法、服务器控件方法、html控件方法等。
1 ///******************* 说明 ***************************/// 2 /// 作者:清风携夕阳 3 /// 时间:2014-09-29 4 /// 描述:Web服务端控件辅助类,程序开发过程中常用方法 5 ///***************************************************/// 6 using System; 7 using System.Data; 8 using System.Collections.Generic; 9 using System.Web.UI.WebControls; 10 namespace Common 11 { 12 /// <summary> 13 /// Web服务端控件辅助类 14 /// </summary> 15 [Serializable] 16 public static class WebHelper 17 { 18 #region 常量、静态变量 19 /// <summary> 20 /// 8位时间格式yyyymmdd 21 /// </summary> 22 public static string time8 = "yyyymmdd"; 23 /// <summary> 24 /// 10位时间格式yyyy-mm-dd 25 /// </summary> 26 public static string time10 = "yyyy-mm-dd"; 27 /// <summary> 28 /// 通用空值选项文本 29 /// </summary> 30 public static string emptySelect = "--请选择--"; 31 #endregion 32 #region 验证、检测方法 33 /// <summary> 34 /// 验证sql匹配条件是否正确(若以and开头则自动去除) 35 /// </summary> 36 /// <param name="strWhere">sql匹配条件</param> 37 public static string CheckStrWhere(string strWhere) 38 { 39 string str = strWhere.TrimStart();//去除前置空格 40 if (str.ToLower().IndexOf("and ") == 0)//若以and开头则自动去除第一个and 41 { 42 strWhere = str.Substring(4);//若要保留前面一个空格,可以改为3 43 } 44 return strWhere; 45 } 46 #endregion 47 #region 服务端控件方法 48 49 #region CheckBoxList 50 /// <summary> 51 /// 获取CheckBoxList选中项数目 52 /// </summary> 53 public static int CheckedCount(CheckBoxList ckboxlist) 54 { 55 int count = 0; 56 foreach (ListItem item in ckboxlist.Items) 57 { 58 if (item.Selected == true) 59 { 60 count++; 61 } 62 } 63 return count; 64 } 65 /// <summary> 66 /// 根据选项值选中CheckBoxList选项 67 /// </summary> 68 public static void SetChecked(CheckBoxList cboxlist, List<string> vals) 69 { 70 if (vals == null || vals.Count == 0) 71 { 72 return; 73 } 74 for (int i = 0; i < cboxlist.Items.Count; i++) 75 { 76 ListItem item = cboxlist.Items[i]; 77 for (int j = 0; j < vals.Count; j++) 78 { 79 if (item.Value == vals[j]) 80 { 81 item.Selected = true; 82 vals.Remove(vals[j]); 83 break; 84 } 85 } 86 if (vals.Count == 0) 87 { 88 return; 89 } 90 } 91 } 92 /// <summary> 93 /// 获取CheckBoxList选中项的值 94 /// </summary> 95 public static List<string> GetChecked(CheckBoxList cboxlist) 96 { 97 List<string> vals = new List<string>(); 98 foreach (ListItem item in cboxlist.Items) 99 {100 if (item.Selected == true)101 {102 vals.Add(item.Value);103 }104 }105 return vals;106 }107 /// <summary>108 /// 清空选项109 /// </summary>110 public static void ClearChecked(CheckBoxList cboxlist)111 {112 foreach (ListItem item in cboxlist.Items)113 {114 item.Selected = false;115 }116 }117 /// <summary>118 /// 全选119 /// </summary>120 public static void CheckAll(CheckBoxList cboxlist)121 {122 foreach (ListItem item in cboxlist.Items)123 {124 item.Selected = true;125 }126 }127 /// <summary>128 /// 反选129 /// </summary>130 public static void CheckNotChecked(CheckBoxList cboxlist)131 {132 foreach (ListItem item in cboxlist.Items)133 {134 item.Selected = !item.Selected;135 }136 }137 /// <summary>138 /// 根据数据表绑定CheckBoxList控件139 /// </summary>140 /// <param name="dt">数据表</param>141 /// <param name="TextField">选项名称列编码</param>142 /// <param name="ValueField">选项值列编码</param>143 public static void BindCheckBoxList(CheckBoxList cboxlist, DataTable dt, string TextField, string ValueField)144 {145 cboxlist.Items.Clear();146 if (dt != null && dt.Rows.Count > 0)147 {148 cboxlist.DataSource = dt;149 cboxlist.DataTextField = TextField;150 cboxlist.DataValueField = ValueField;151 cboxlist.DataBind();152 }153 }154 #endregion155 #region RadioButtonList156 /// <summary>157 /// 根据数据表绑定RadioButtonList控件158 /// </summary>159 /// <param name="dt">数据</param>160 /// <param name="TextField">选项名称列编码</param>161 /// <param name="ValueField">选项值列编码</param>162 public static void BindRadioButtonList(RadioButtonList rdolist, DataTable dt, string TextField, string ValueField)163 {164 rdolist.Items.Clear();165 if (dt != null && dt.Rows.Count > 0)166 {167 rdolist.DataSource = dt;168 rdolist.DataTextField = TextField;169 rdolist.DataValueField = ValueField;170 rdolist.DataBind();171 }172 }173 #endregion174 #region DropDownList175 /// <summary>176 /// 根据数据表绑定RadioButtonList控件177 /// </summary>178 /// <param name="dt">数据表</param>179 /// <param name="TextField">选项名称列编码</param>180 /// <param name="ValueField">选项值列编码</param>181 /// <param name="ListName">空值显示文本,若为空则无空值选项</param>182 public static void BindDropDownList(DropDownList dlist, DataTable dt, string TextField, string ValueField, string EmptyValueText)183 {184 dlist.Items.Clear();185 if (dt != null && dt.Rows.Count > 0)186 {187 dlist.DataSource = dt;188 dlist.DataTextField = TextField;189 dlist.DataValueField = ValueField;190 dlist.DataBind();191 }192 if (!String.IsNullOrEmpty(EmptyValueText))193 {194 dlist.Items.Insert(0, new ListItem(EmptyValueText, ""));195 }196 }197 #endregion198 #region ListBox199 /// <summary>200 /// 根据数据表绑定ListBox控件201 /// </summary>202 /// <param name="dt">数据表</param>203 /// <param name="TextField">选项名称列编码</param>204 /// <param name="ValueField">选项值列编码</param>205 public static void BindListBox(ListBox lbox, DataTable dt, string TextField, string ValueField)206 {207 lbox.Items.Clear();208 if (dt != null && dt.Rows.Count > 0)209 {210 lbox.DataSo