///******************* 说明 ***************************/// /// 作者:清风携夕阳 /// 时间:2014-09-29 /// 描述:Web服务端控件辅助类,程序开发过程中常用方法 ///***************************************************/// using System; using System.Data; using System.Collections.Generic; using System.Web.UI.WebControls; namespace Common { ////// Web服务端控件辅助类 /// [Serializable] public static class WebHelper { #region 常量、静态变量 /// /// 8位时间格式yyyymmdd /// public static string time8 = "yyyymmdd"; /// /// 10位时间格式yyyy-mm-dd /// public static string time10 = "yyyy-mm-dd"; /// /// 通用空值选项文本 /// public static string emptySelect = "--请选择--"; #endregion #region 验证、检测方法 /// /// 验证sql匹配条件是否正确(若以and开头则自动去除) /// /// sql匹配条件 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 /// /// 获取CheckBoxList选中项数目 /// public static int CheckedCount(CheckBoxList ckboxlist) { int count = 0; foreach (ListItem item in ckboxlist.Items) { if (item.Selected == true) { count++; } } return count; } /// /// 根据选项值选中CheckBoxList选项 /// public static void SetChecked(CheckBoxList cboxlist, List<string> vals) { if (vals == null || vals.Count == 0) { return; } for (int i = 0; i ) { ListItem item = cboxlist.Items[i]; for (int j = 0; j ) { if (item.Value == vals[j]) { item.Selected = true; vals.Remove(vals[j]); break; } } if (vals.Count == 0) { return; } } } /// /// 获取CheckBoxList选中项的值 /// 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; } /// /// 清空选项 /// public static void ClearChecked(CheckBoxList cboxlist) { foreach (ListItem item in cboxlist.Items) { item.Selected = false; } } /// /// 全选 /// public static void CheckAll(CheckBoxList cboxlist) { foreach (ListItem item in cboxlist.Items) { item.Selected = true; } } /// /// 反选 /// public static void CheckNotChecked(CheckBoxList cboxlist) { foreach (ListItem item in cboxlist.Items) { item.Selected = !item.Selected; } } /// /// 根据数据表绑定CheckBoxList控件 /// /// 数据表 /// 选项名称列编码 /// 选项值列编码 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 /// /// 根据数据表绑定RadioButtonList控件 /// /// 数据 /// 选项名称列编码 /// 选项值列编码 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 /// /// 根据数据表绑定RadioButtonList控件 /// /// 数据表 /// 选项名称列编码 /// 选项值列编码 /// 空值显示文本,若为空则无空值选项 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 /// /// 根据数据表绑定ListBox控件 /// /// 数据表 /// 选项名称列编码 /// 选项值列编码 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(); } } /// /// 根据选项文本查找并选中ListBox选项 /// /// ListBox /// 选项显示的文本 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 ) { 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 /// /// 展开指定节点的所有上级节点 /// public static void ExpandAllParentNode(TreeNode tn) { if (tn.Parent != null) { tn.Parent.Expand(); ExpandAllParentNode(tn.Parent); } } /// /// 清空TreeView节点选中状态 /// public static void ClearTreeNodesChecked(TreeView tview) { if (tview.Nodes.Count > 0) { foreach (TreeNode tn in tview.Nodes) { ClearTreeNodesChecked(tn); } } } /// /// 清空子节点选中状态 /// 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); } } } } /// /// 根据节点Value值查找节点 /// /// 根节点 /// 节点值 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; } /// /// 根据节点Value值查找节点 /// /// TreeView /// 节点值 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; } /// /// 根据节点Value值查找指定层级的节点 /// /// 根节点 /// 节点层级 /// 节点值 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 //不去查找更深层次的节点 { foreach (TreeNode tn in tnParent.ChildNodes) { tnRet = FindNodeByValue(tn, depth, strValue); if (tnRet != null) break; } } return tnRet; } /// /// 根据节点Value值查找指定层级的节点 /// /// TreeView /// 节点层级 /// 节点值 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; } /// /// 根据节点显示名称查找节点 /// /// 根节点 /// 节点显示名称 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; } /// /// 根据节点显示名称查找节点 /// /// TreeView /// 节点显示名称 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; } /// /// 根据节点显示名称查找指定层级的节点 /// /// 根节点 /// 节点层级 /// 节点显示名称 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 //不去查找更深层级的节点 { foreach (TreeNode tn in tnParent.ChildNodes) { tnRet = FindNodeByText(tn, depth, strValue); if (tnRet != null) break; } } return tnRet; } /// /// 根据节点显示名称查找指定层级的节点 /// /// TreeView /// 节点层级 /// 节点显示名称 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; } /// /// 根据节点Value值选中指定层级的节点 /// /// 节点层级 /// 节点值 public static TreeNode CheckNodeByValue(TreeView tview, int depth, string strValue) { TreeNode tn = FindNodeByValue(tview, depth, strValue); if (tn != null) { tn.Checked = true; } return tn; } /// /// 根据节点显示名称选中指定层级的节点 /// /// TreeView /// 节点层级 /// 节点显示名称 public static TreeNode CheckNodeByText(TreeView tview, int depth, string strValue) { TreeNode tn = FindNodeByText(tview, depth, strValue); if (tn != null) { tn.Checked = true; } return tn; } /// /// 根据节点Value值查找并选定节点 /// /// 节点值 public static TreeNode FixNodeByValue(TreeView tview, string strValue) { TreeNode tn = FindNodeByValue(tview, strValue); if (tn != null) { ExpandAllParentNode(tn); tn.Select(); } return tn; } /// /// 根据节点显示名称查找并选定节点 /// /// TreeView /// 节点显示名称 public static TreeNode FixNodeByText(TreeView tview, string strValue) { TreeNode tn = FindNodeByText(tview, strValue); if (tn != null) { ExpandAllParentNode(tn); tn.Select(); } return tn; } /// /// 展开第一序列节点并选中最底层节点 /// /// 根节点 /// tview public static void ExpandFirstsNode(TreeNode root, TreeView tview) { if (root.ChildNodes.Count > 0) { ExpandFirstsNode(root.ChildNodes[0], tview); } else { root.Select(); } } /// /// 展开第一序列节点并选中最底层节点 /// public static void ExpandFirstsNode(TreeView tview) { if (tview.Nodes.Count > 0) { ExpandFirstsNode(tview.Nodes[0], tview); } } #endregion #endregion #region html控件方法 #region select /// /// 获取下拉选项htm /// /// 数据集 /// 选项值字段 /// 选项文本字段 /// 空值文本,若为空则无空值选项 public static string GetSelectOptionHtm(DataTable dt, string valueField, string textField, string emptyText) { string htm = String.Empty; if (!String.IsNullOrEmpty(emptyText)) { htm += "" + emptyText + "\r\n"; } if (dt != null) { for (int i = 0; i ) { htm += "\r\n"; } } return htm; } /// /// 绑定下拉列表(runat=‘server‘的select) /// /// 数据集 /// 选项值字段 /// 选项文本字段 /// 空值文本,若为空则无空值选项 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 } }
asp.net中的WebFrom