///******************* 说明 ***************************/// /// 作者:清风携夕阳 /// 时间:2014-09-29 /// 描述:Web服务端控件辅助类,程序开发过程中常用方法 ///***************************************************/// using System; using System.Data; using System.Collections.Generic; using System.Web.UI.WebControls; namespace Common { /// <summary> /// Web服务端控件辅助类 /// </summary> [Serializable] public static class WebHelper { #region 常量、静态变量 /// <summary> /// 8位时间格式yyyymmdd /// </summary> public static string time8 = "yyyymmdd"; /// <summary> /// 10位时间格式yyyy-mm-dd /// </summary> public static string time10 = "yyyy-mm-dd"; /// <summary> /// 通用空值选项文本 /// </summary> public static string emptySelect = "--请选择--"; #endregion #region 验证、检测方法 /// <summary> /// 验证sql匹配条件是否正确(若以and开头则自动去除) /// </summary> /// <param name="strWhere">sql匹配条件</param> public static string CheckStrWhere(string strWhere) { string str = strWhere.TrimStart();//去除前置空格 if (str.ToLower().IndexOf("and ") == 0)//若以and开头则自动去除第一个and { strWhere = str.Substring(4);//若要保留前面一个空格,可以改为3 } return strWhere; } #endregion #region 服务端控件方法 #region CheckBoxList /// <summary> /// 获取CheckBoxList选中项数目 /// </summary> public static int CheckedCount(CheckBoxList ckboxlist) { int count = 0; foreach (ListItem item in ckboxlist.Items) { if (item.Selected == true) { count++; } } return count; } /// <summary> /// 根据选项值选中CheckBoxList选项 /// </summary> public static void SetChecked(CheckBoxList cboxlist, List<string> vals) { if (vals == null || vals.Count == 0) { return; } for (int i = 0; i < cboxlist.Items.Count; i++) { ListItem item = cboxlist.Items[i]; for (int j = 0; j < vals.Count; j++) { if (item.Value == vals[j]) { item.Selected = true; vals.Remove(vals[j]); break; } } if (vals.Count == 0) { return; } } } /// <summary> /// 获取CheckBoxList选中项的值 /// </summary> public static List<string> GetChecked(CheckBoxList cboxlist) { List<string> vals = new List<string>(); foreach (ListItem item in cboxlist.Items) { if (item.Selected == true) { vals.Add(item.Value); } } return vals; } /// <summary> /// 清空选项 /// </summary> public static void ClearChecked(CheckBoxList cboxlist) { foreach (ListItem item in cboxlist.Items) { item.Selected = false; } } /// <summary> /// 全选 /// </summary> public static void CheckAll(CheckBoxList cboxlist) { foreach (ListItem item in cboxlist.Items) { item.Selected = true; } } /// <summary> /// 反选 /// </summary> public static void CheckNotChecked(CheckBoxList cboxlist) { foreach (ListItem item in cboxlist.Items) { item.Selected = !item.Selected; } } /// <summary> /// 根据数据表绑定CheckBoxList控件 /// </summary> /// <param name="dt">数据表</param> /// <param name="TextField">选项名称列编码</param> /// <param name="ValueField">选项值列编码</param> public static void BindCheckBoxList(CheckBoxList cboxlist, DataTable dt, string TextField, string ValueField) { cboxlist.Items.Clear(); if (dt != null && dt.Rows.Count > 0) { cboxlist.DataSource = dt; cboxlist.DataTextField = TextField; cboxlist.DataValueField = ValueField; cboxlist.DataBind(); } } #endregion #region RadioButtonList /// <summary> /// 根据数据表绑定RadioButtonList控件 /// </summary> /// <param name="dt">数据</param> /// <param name="TextField">选项名称列编码</param> /// <param name="ValueField">选项值列编码</param> public static void BindRadioButtonList(RadioButtonList rdolist, DataTable dt, string TextField, string ValueField) { rdolist.Items.Clear(); if (dt != null && dt.Rows.Count > 0) { rdolist.DataSource = dt; rdolist.DataTextField = TextField; rdolist.DataValueField = ValueField; rdolist.DataBind(); } } #endregion #region DropDownList /// <summary> /// 根据数据表绑定RadioButtonList控件 /// </summary> /// <param name="dt">数据表</param> /// <param name="TextField">选项名称列编码</param> /// <param name="ValueField">选项值列编码</param> /// <param name="ListName">空值显示文本,若为空则无空值选项</param> public static void BindDropDownList(DropDownList dlist, DataTable dt, string TextField, string ValueField, string EmptyValueText) { dlist.Items.Clear(); if (dt != null && dt.Rows.Count > 0) { dlist.DataSource = dt; dlist.DataTextField = TextField; dlist.DataValueField = ValueField; dlist.DataBind(); } if (!String.IsNullOrEmpty(EmptyValueText)) { dlist.Items.Insert(0, new ListItem(EmptyValueText, "")); } } #endregion #region ListBox /// <summary> /// 根据数据表绑定ListBox控件 /// </summary> /// <param name="dt">数据表</param> /// <param name="TextField">选项名称列编码</param> /// <param name="ValueField">选项值列编码</param> public static void BindListBox(ListBox lbox, DataTable dt, string TextField, string ValueField) { lbox.Items.Clear(); if (dt != null && dt.Rows.Count > 0) { lbox.DataSource = dt; lbox.DataTextField = TextField; lbox.DataValueField = ValueField; lbox.DataBind(); } } /// <summary> /// 根据选项文本查找并选中ListBox选项 /// </summary> /// <param name="lbox">ListBox</param> /// <param name="strValue">选项显示的文本</param> public static void FindAndFixItemByText(ListBox lbox, string strValue) { int count = lbox.Items.Count; int index = lbox.SelectedIndex; if (count > 0) { int i = index + 1; for (; i < count; i++) { ListItem li = lbox.Items[i]; if (li.Text.Contains(strValue)) { lbox.SelectedIndex = i; break; } if (index > 0 && i == count - 1) { count = index; i = 0; index = 0; } } } } #endregion #region TreeView 2013-08-12 /// <summary> /// 展开指定节点的所有上级节点 /// </summary> public static void ExpandAllParentNode(TreeNode tn) { if (tn.Parent != null) { tn.Parent.Expand(); ExpandAllParentNode(tn.Parent); } } /// <summary> /// 清空TreeView节点选中状态 /// </summary> public static void ClearTreeNodesChecked(TreeView tview) { if (tview.Nodes.Count > 0) { foreach (TreeNode tn in tview.Nodes) { ClearTreeNodesChecked(tn); } } } /// <summary> /// 清空子节点选中状态 /// </summary> public static void ClearTreeNodesChecked(TreeNode tn) { if (tn != null) { tn.Checked = false; if (tn.ChildNodes.Count > 0) { foreach (TreeNode child in tn.ChildNodes) { ClearTreeNodesChecked(child); } } } } /// <summary> /// 根据节点Value值查找节点 /// </summary> /// <param name="tnParent">根节点</param> /// <param name="strValue">节点值</param> public static TreeNode FindNodeByValue(TreeNode tnParent, string strValue) { if (tnParent == null) return null; if (tnParent.Value == strValue) return tnParent; TreeNode tnRet = null; foreach (TreeNode tn in tnParent.ChildNodes) { tnRet = FindNodeByValue(tn, strValue); if (tnRet != null) break; } return tnRet; } /// <summary> /// 根据节点Value值查找节点 /// </summary> /// <param name="tview">TreeView</param> /// <param name="strValue">节点值</param> public static TreeNode FindNodeByValue(TreeView tview, string strValue) { if (tview.Nodes.Count == 0) return null; TreeNode tnRet = null; foreach (TreeNode tn in tview.Nodes) { tnRet = FindNodeByValue(tn, strValue); if (tnRet != null) break; } return tnRet; } /// <summary> /// 根据节点Value值查找指定层级的节点 /// </summary> /// <param name="tnParent">根节点</param> /// <param name="depth">节点层级</param> /// <param name="strValue">节点值</param> public static TreeNode FindNodeByValue(TreeNode tnParent, int depth, string strValue) { if (tnParent == null) return null; if (tnParent.Value == strValue && tnParent.Depth == depth) return tnParent; TreeNode tnRet = null; if (tnParent.Depth < depth)//不去查找更深层次的节点 { foreach (TreeNode tn in tnParent.ChildNodes) { tnRet = FindNodeByValue(tn, depth, strValue); if (tnRet != null) break; } } return tnRet; } /// <summary> /// 根据节点Value值查找指定层级的节点 /// </summary> /// <param name="tview">TreeView</param> /// <param name="depth">节点层级</param> /// <param name="strValue">节点值</param> public static TreeNode FindNodeByValue(TreeView tview, int depth, string strValue) { if (tview.Nodes.Count == 0) return null; TreeNode tnRet = null; foreach (TreeNode tn in tview.Nodes) { tnRet = FindNodeByValue(tn, depth, strValue); if (tnRet != null) break; } return tnRet; } /// <summary> /// 根据节点显示名称查找节点 /// </summary> /// <param name="tnParent">根节点</param> /// <param name="strValue">节点显示名称</param> public static TreeNode FindNodeByText(TreeNode tnParent, string strValue) { if (tnParent == null) return null; if (tnParent.Text == strValue) return tnParent; TreeNode tnRet = null; foreach (TreeNode tn in tnParent.ChildNodes) { tnRet = FindNodeByText(tn, strValue); if (tnRet != null) break; } return tnRet; } /// <summary> /// 根据节点显示名称查找节点 /// </summary> /// <param name="tview">TreeView</param> /// <param name="strValue">节点显示名称</param> public static TreeNode FindNodeByText(TreeView tview, string strValue) { if (tview.Nodes.Count == 0) return null; TreeNode tnRet = null; foreach (TreeNode tn in tview.Nodes) { tnRet = FindNodeByText(tn, strValue); if (tnRet != null) break; } return tnRet; } /// <summary> /// 根据节点显示名称查找指定层级的节点 /// </summary> /// <param name="tnParent">根节点</param> /// <param name="depth">节点层级</param> /// <param name="strValue">节点显示名称</param> public static TreeNode FindNodeByText(TreeNode tnParent, int depth, string strValue) { if (tnParent == null) return null; if (tnParent.Text == strValue && tnParent.Depth == depth) return tnParent; TreeNode tnRet = null; if (tnParent.Depth < depth)//不去查找更深层级的节点 { foreach (TreeNode tn in tnParent.ChildNodes) { tnRet = FindNodeByText(tn, depth, strValue); if (tnRet != null) break; } } return tnRet; } /// <summary> /// 根据节点显示名称查找指定层级的节点 /// </summary> /// <param name="tview">TreeView</param> /// <param name="depth">节点层级</param> /// <param name="strValue">节点显示名称</param> public static TreeNode FindNodeByText(TreeView tview, int depth, string strValue) { if (tview.Nodes.Count == 0) return null; TreeNode tnRet = null; foreach (TreeNode tn in tview.Nodes) { tnRet = FindNodeByText(tn, depth, strValue); if (tnRet != null) break; } return tnRet; } /// <summary> /// 根据节点Value值选中指定层级的节点 /// </summary> /// <param name="depth">节点层级</param> /// <param name="strValue">节点值</param> public static TreeNode CheckNodeByValue(TreeView tview, int depth, string strValue) { TreeNode tn = FindNodeByValue(tview, depth, strValue); if (tn != null) { tn.Checked = true; } return tn; } /// <summary> /// 根据节点显示名称选中指定层级的节点 /// </summary> /// <param name="tview">TreeView</param> /// <param name="depth">节点层级</param> /// <param name="strValue">节点显示名称</param> public static TreeNode CheckNodeByText(TreeView tview, int depth, string strValue) { TreeNode tn = FindNodeByText(tview, depth, strValue); if (tn != null) { tn.Checked = true; } return tn; } /// <summary> /// 根据节点Value值查找并选定节点 /// </summary> /// <param name="strValue">节点值</param> public static TreeNode FixNodeByValue(TreeView tview, string strValue) { TreeNode tn = FindNodeByValue(tview, strValue); if (tn != null) { ExpandAllParentNode(tn); tn.Select(); } return tn; } /// <summary> /// 根据节点显示名称查找并选定节点 /// </summary> /// <param name="tview">TreeView</param> /// <param name="strValue">节点显示名称</param> public static TreeNode FixNodeByText(TreeView tview, string strValue) { TreeNode tn = FindNodeByText(tview, strValue); if (tn != null) { ExpandAllParentNode(tn); tn.Select(); } return tn; } /// <summary> /// 展开第一序列节点并选中最底层节点 /// </summary> /// <param name="root">根节点</param> /// <param name="tview">tview</param> public static void ExpandFirstsNode(TreeNode root, TreeView tview) { if (root.ChildNodes.Count > 0) { ExpandFirstsNode(root.ChildNodes[0], tview); } else { root.Select(); } } /// <summary> /// 展开第一序列节点并选中最底层节点 /// </summary> public static void ExpandFirstsNode(TreeView tview) { if (tview.Nodes.Count > 0) { ExpandFirstsNode(tview.Nodes[0], tview); } } #endregion #endregion #region html控件方法 #region select /// <summary> /// 获取下拉选项htm /// </summary> /// <param name="dt">数据集</param> /// <param name="valueField">选项值字段</param> /// <param name="textField">选项文本字段</param> /// <param name="emptyText">空值文本,若为空则无空值选项</param> public static string GetSelectOptionHtm(DataTable dt, string valueField, string textField, string emptyText) { string htm = String.Empty; if (!String.IsNullOrEmpty(emptyText)) { htm += "<option value=\"\">" + emptyText + "</option>\r\n"; } if (dt != null) { for (int i = 0; i < dt.Rows.Count; i++) { htm += "<option value=\"" + dt.Rows[i][valueField] + "\">" + dt.Rows[i][textField] + "</option>\r\n"; } } return htm; } /// <summary> /// 绑定下拉列表(runat=‘server‘的select) /// </summary> /// <param name="dt">数据集</param> /// <param name="valueField">选项值字段</param> /// <param name="textField">选项文本字段</param> /// <param name="emptyText">空值文本,若为空则无空值选项</param> public static void BindSelectList(DataTable dt,HtmlSelect select,string valueField,string textField,string emptyText) { select.Items.Clear(); if (dt != null && dt.Rows.Count > 0) { select.DataSource = dt; select.DataValueField = valueField; select.DataTextField = textField; select.DataBind(); } if (!String.IsNullOrEmpty(emptyText)) { select.Items.Insert(0, new System.Web.UI.WebControls.ListItem(emptyText, "")); } } #endregion #endregion } }
B/S项目常用方法
时间: 2024-10-30 10:52:31