首页 > 代码库 > BS常用方法备忘

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 =http://www.mamicode.com/= 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.DataSource = dt;211                 lbox.DataTextField = TextField;212                 lbox.DataValueField = ValueField;213                 lbox.DataBind();214             }215         }216         /// <summary>217         /// 根据选项文本查找并选中ListBox选项218         /// </summary>219         /// <param name="lbox">ListBox</param>220         /// <param name="strValue">选项显示的文本</param>221         public static void FindAndFixItemByText(ListBox lbox, string strValue)222         {223             int count = lbox.Items.Count;224             int index = lbox.SelectedIndex;225             if (count > 0)226             {227                 int i = index + 1;228                 for (; i < count; i++)229                 {230                     ListItem li = lbox.Items[i];231                     if (li.Text.Contains(strValue))232                     {233                         lbox.SelectedIndex = i;234                         break;235                     }236                     if (index > 0 && i == count - 1)237                     {238                         count = index;239                         i = 0;240                         index = 0;241                     }242                 }243             }244         }245         #endregion246         #region TreeView 2013-08-12247         /// <summary>248         /// 展开指定节点的所有上级节点249         /// </summary>250         public static void ExpandAllParentNode(TreeNode tn)251         {252             if (tn.Parent != null)253             {254                 tn.Parent.Expand();255                 ExpandAllParentNode(tn.Parent);256             }257         }258         /// <summary>259         /// 清空TreeView节点选中状态260         /// </summary>261         public static void ClearTreeNodesChecked(TreeView tview)262         {263             if (tview.Nodes.Count > 0)264             {265                 foreach (TreeNode tn in tview.Nodes)266                 {267                     ClearTreeNodesChecked(tn);268                 }269             }270         }271         /// <summary>272         /// 清空子节点选中状态273         /// </summary>274         public static void ClearTreeNodesChecked(TreeNode tn)275         {276             if (tn != null)277             {278                 tn.Checked = false;279                 if (tn.ChildNodes.Count > 0)280                 {281                     foreach (TreeNode child in tn.ChildNodes)282                     {283                         ClearTreeNodesChecked(child);284                     }285                 }286             }287         }288         /// <summary>289         /// 根据节点Value值查找节点290         /// </summary>291         /// <param name="tnParent">根节点</param>292         /// <param name="strValue">节点值</param>293         public static TreeNode FindNodeByValue(TreeNode tnParent, string strValue)294         {295             if (tnParent == null)296                 return null;297             if (tnParent.Value =http://www.mamicode.com/= strValue)298                 return tnParent;299             TreeNode tnRet = null;300             foreach (TreeNode tn in tnParent.ChildNodes)301             {302                 tnRet = FindNodeByValue(tn, strValue);303                 if (tnRet != null) break;304             }305             return tnRet;306         }307         /// <summary>308         /// 根据节点Value值查找节点309         /// </summary>310         /// <param name="tview">TreeView</param>311         /// <param name="strValue">节点值</param>312         public static TreeNode FindNodeByValue(TreeView tview, string strValue)313         {314             if (tview.Nodes.Count == 0)315                 return null;316             TreeNode tnRet = null;317             foreach (TreeNode tn in tview.Nodes)318             {319                 tnRet = FindNodeByValue(tn, strValue);320                 if (tnRet != null) break;321             }322             return tnRet;323         }324         /// <summary>325         /// 根据节点Value值查找指定层级的节点326         /// </summary>327         /// <param name="tnParent">根节点</param>328         /// <param name="depth">节点层级</param>329         /// <param name="strValue">节点值</param>330         public static TreeNode FindNodeByValue(TreeNode tnParent, int depth, string strValue)331         {332             if (tnParent == null)333                 return null;334             if (tnParent.Value =http://www.mamicode.com/= strValue && tnParent.Depth == depth)335                 return tnParent;336             TreeNode tnRet = null;337             if (tnParent.Depth < depth)//不去查找更深层次的节点338             {339                 foreach (TreeNode tn in tnParent.ChildNodes)340                 {341                     tnRet = FindNodeByValue(tn, depth, strValue);342                     if (tnRet != null) break;343                 }344             }345             return tnRet;346         }347         /// <summary>348         /// 根据节点Value值查找指定层级的节点349         /// </summary>350         /// <param name="tview">TreeView</param>351         /// <param name="depth">节点层级</param>352         /// <param name="strValue">节点值</param>353         public static TreeNode FindNodeByValue(TreeView tview, int depth, string strValue)354         {355             if (tview.Nodes.Count == 0)356                 return null;357             TreeNode tnRet = null;358             foreach (TreeNode tn in tview.Nodes)359             {360                 tnRet = FindNodeByValue(tn, depth, strValue);361                 if (tnRet != null) break;362             }363             return tnRet;364         }365         /// <summary>366         /// 根据节点显示名称查找节点367         /// </summary>368         /// <param name="tnParent">根节点</param>369         /// <param name="strValue">节点显示名称</param>370         public static TreeNode FindNodeByText(TreeNode tnParent, string strValue)371         {372             if (tnParent == null)373                 return null;374             if (tnParent.Text == strValue)375                 return tnParent;376             TreeNode tnRet = null;377             foreach (TreeNode tn in tnParent.ChildNodes)378             {379                 tnRet = FindNodeByText(tn, strValue);380                 if (tnRet != null) break;381             }382             return tnRet;383         }384         /// <summary>385         /// 根据节点显示名称查找节点386         /// </summary>387         /// <param name="tview">TreeView</param>388         /// <param name="strValue">节点显示名称</param>389         public static TreeNode FindNodeByText(TreeView tview, string strValue)390         {391             if (tview.Nodes.Count == 0)392                 return null;393             TreeNode tnRet = null;394             foreach (TreeNode tn in tview.Nodes)395             {396                 tnRet = FindNodeByText(tn, strValue);397                 if (tnRet != null) break;398             }399             return tnRet;400         }401         /// <summary>402         /// 根据节点显示名称查找指定层级的节点403         /// </summary>404         /// <param name="tnParent">根节点</param>405         /// <param name="depth">节点层级</param>406         /// <param name="strValue">节点显示名称</param>407         public static TreeNode FindNodeByText(TreeNode tnParent, int depth, string strValue)408         {409             if (tnParent == null)410                 return null;411             if (tnParent.Text == strValue && tnParent.Depth == depth)412                 return tnParent;413             TreeNode tnRet = null;414             if (tnParent.Depth < depth)//不去查找更深层级的节点415             {416                 foreach (TreeNode tn in tnParent.ChildNodes)417                 {418                     tnRet = FindNodeByText(tn, depth, strValue);419                     if (tnRet != null) break;420                 }421             }422             return tnRet;423         }424         /// <summary>425         /// 根据节点显示名称查找指定层级的节点426         /// </summary>427         /// <param name="tview">TreeView</param>428         /// <param name="depth">节点层级</param>429         /// <param name="strValue">节点显示名称</param>430         public static TreeNode FindNodeByText(TreeView tview, int depth, string strValue)431         {432             if (tview.Nodes.Count == 0)433                 return null;434             TreeNode tnRet = null;435             foreach (TreeNode tn in tview.Nodes)436             {437                 tnRet = FindNodeByText(tn, depth, strValue);438                 if (tnRet != null) break;439             }440             return tnRet;441         }442         /// <summary>443         /// 根据节点Value值选中指定层级的节点444         /// </summary>445         /// <param name="depth">节点层级</param>446         /// <param name="strValue">节点值</param>447         public static TreeNode CheckNodeByValue(TreeView tview, int depth, string strValue)448         {449             TreeNode tn = FindNodeByValue(tview, depth, strValue);450             if (tn != null)451             {452                 tn.Checked = true;453             }454             return tn;455         }456         /// <summary>457         /// 根据节点显示名称选中指定层级的节点458         /// </summary>459         /// <param name="tview">TreeView</param>460         /// <param name="depth">节点层级</param>461         /// <param name="strValue">节点显示名称</param>462         public static TreeNode CheckNodeByText(TreeView tview, int depth, string strValue)463         {464             TreeNode tn = FindNodeByText(tview, depth, strValue);465             if (tn != null)466             {467                 tn.Checked = true;468             }469             return tn;470         }471         /// <summary>472         /// 根据节点Value值查找并选定节点473         /// </summary>474         /// <param name="strValue">节点值</param>475         public static TreeNode FixNodeByValue(TreeView tview, string strValue)476         {477             TreeNode tn = FindNodeByValue(tview, strValue);478             if (tn != null)479             {480                 ExpandAllParentNode(tn);481                 tn.Select();482             }483             return tn;484         }485         /// <summary>486         /// 根据节点显示名称查找并选定节点487         /// </summary>488         /// <param name="tview">TreeView</param>489         /// <param name="strValue">节点显示名称</param>490         public static TreeNode FixNodeByText(TreeView tview, string strValue)491         {492             TreeNode tn = FindNodeByText(tview, strValue);493             if (tn != null)494             {495                 ExpandAllParentNode(tn);496                 tn.Select();497             }498             return tn;499         }500         /// <summary>501         /// 展开第一序列节点并选中最底层节点502         /// </summary>503         /// <param name="root">根节点</param>504         /// <param name="tview">tview</param>505         public static void ExpandFirstsNode(TreeNode root, TreeView tview)506         {507             if (root.ChildNodes.Count > 0)508             {509                 ExpandFirstsNode(root.ChildNodes[0], tview);510             }511             else512             {513                 root.Select();514             }515         }516         /// <summary>517         /// 展开第一序列节点并选中最底层节点518         /// </summary>519         public static void ExpandFirstsNode(TreeView tview)520         {521             if (tview.Nodes.Count > 0)522             {523                 ExpandFirstsNode(tview.Nodes[0], tview);524             }525         }526         #endregion527 528         #endregion529         #region html控件方法530 531         #region select532          /// <summary>533         /// 获取下拉选项htm534         /// </summary>535         /// <param name="dt">数据集</param>536         /// <param name="valueField">选项值字段</param>537         /// <param name="textField">选项文本字段</param>538         /// <param name="emptyText">空值文本,若为空则无空值选项</param>539         public static string GetSelectOptionHtm(DataTable dt, string valueField, string textField, string emptyText)540         {541             string htm = String.Empty;542             if (!String.IsNullOrEmpty(emptyText))543             {544                 htm += "<option value=http://www.mamicode.com/"\">" + emptyText + "</option>\r\n";545             }546             if (dt != null)547             {548                 for (int i = 0; i < dt.Rows.Count; i++)549                 {550                     htm += "<option value=http://www.mamicode.com/"" + dt.Rows[i][valueField] + "\">" + dt.Rows[i][textField] + "</option>\r\n";551                 }552             }553             return htm;554         }555         /// <summary>556         /// 绑定下拉列表(runat=‘server‘的select)557         /// </summary>558         /// <param name="dt">数据集</param>559         /// <param name="valueField">选项值字段</param>560         /// <param name="textField">选项文本字段</param>561         /// <param name="emptyText">空值文本,若为空则无空值选项</param>562         public static void BindSelectList(DataTable dt,HtmlSelect select,string valueField,string textField,string emptyText)563         {564             select.Items.Clear();565             if (dt != null && dt.Rows.Count > 0)566             {567                 select.DataSource = dt;568                 select.DataValueField = valueField;569                 select.DataTextField = textField;570                 select.DataBind();571             }572             if (!String.IsNullOrEmpty(emptyText))573             {574                 select.Items.Insert(0, new System.Web.UI.WebControls.ListItem(emptyText, ""));575             }576         }577         #endregion578 579         #endregion580     }581 }
View Code

 

BS常用方法备忘