using System; using System.Collections.Generic; using System.Linq; using System.Text; using ETDExtUI; using ETD.Common; public static class DExt { #region 设置控件值 /// /// 设置控件值 /// /// 控件 /// 值 public static void SetValue(this Label control, string val) { if (!control.IsNull()) { control.Text = val.IfNull().Trim(); } } /// /// 设置控件值 /// /// 控件 /// 值 public static void SetValue(this TextBox control, string val) { if (!control.IsNull()) { control.Text = val.IfNull().Trim(); } } /// /// 设置控件值 /// /// 控件 /// 值 public static void SetValue(this NumberBox control, object val) { if (!control.IsNull()) { control.Text = val.IfNull().Trim(); } } /// /// 设置控件值 /// /// 控件 /// 值 public static void SetValue(this TextArea control, string val) { if (!control.IsNull()) { control.Text = val.IfNull().Trim(); } } /// /// 设置控件值 /// /// 控件 /// 值 public static void SetValue(this DatePicker control, object val) { if (!control.IsNull()) { if (val is string) { control.Text = val.IfNull().Trim(); return; } if (val is DateTime) { DateTime dt = val.ToDateTime(); if (!dt.Equals(DObject.EMPTY_DATE_TIME)) { control.SelectedDate = dt; } return; } } } /// /// 设置控件值 /// /// 控件 /// 值 public static void SetValue(this CheckBox control, object val) { if (control.IsNull()) { return; } if (val is bool) { control.Checked = (bool)val; return; } if (val.ToString().Equals(DString.NUM_1)) { control.Checked = true; return; } control.Checked = false; } /// /// 设置控件值 /// /// 控件 /// 值 public static void SetValue(this DropDownList control, string val) { if (!control.IsNull()) { control.SelectedValue = val; } } /// /// 设置控件值 /// /// 控件 /// 值 public static void SetValue(this ToolbarText control, string val) { if (!control.IsNull()) { control.Text = val.IfNull().Trim(); } } /// /// 设置控件值 /// /// 控件 /// 值 public static void SetValue(this TwinTriggerBox control, string val) { if (!control.IsNull()) { control.Text = val.IfNull().Trim(); } } /// /// 设置点击 /// /// 控件 /// 值 public static void SetClick(this Button control, string val) { if (!control.IsNull()) { control.OnClientClick = val.IfNull().Trim(); } } /// /// 绑定控件数据 /// /// 控件 /// 数据 public static void SetDllDataSource(this DropDownList control, List data, string selectedvalue) where T : ECustomTree, new() { if (!control.IsNull()) { control.EnableSimulateTree = true; control.DataValueField = DColumn.Id; control.DataTextField = DColumn.NameD; control.DataSimulateTreeLevelField = DColumn.LevelD; control.DataEnableSelectField = DColumn.StatusD; control.DataSource = data; control.DataBind(); control.SetValue(selectedvalue); } } #endregion #region 提取控件值 /// /// 提取控件值 /// /// 控件 /// 值 public static string GetValue(this Label control) { if (!control.IsNull()) { return control.Text.IfNull().Trim(); } return String.Empty; } /// /// 提取控件值 /// /// 控件 /// 值 public static string GetValue(this TextBox control) { if (!control.IsNull()) { return control.Text.IfNull().FilterHTML().Trim(); } return String.Empty; } /// /// 提取控件值 /// /// 控件 /// 值 public static int GetValue(this NumberBox control) { if (!control.IsNull()) { return control.Text.ToInt32(); } return 0; } /// /// 提取控件值 /// /// 控件 /// 值 public static decimal GetDecValue(this NumberBox control) { if (!control.IsNull()) { return control.Text.ToDec(); } return 0; } /// /// 提取控件值 /// /// 控件 /// 值 public static string GetValue(this TextArea control) { if (!control.IsNull()) { return control.Text.IfNull().FilterHTML().Trim(); } return String.Empty; } /// /// 提取控件值 /// /// 控件 /// 值 public static DateTime GetValue(this DatePicker control) { if (!control.IsNull()) { return control.SelectedDate.ToDateTime(); } return DObject.EMPTY_DATE_TIME; } /// /// 提取控件值为空时返回最小 /// /// 控件 /// 值 public static DateTime GetValueEmptyMin(this DatePicker control) { if (!control.IsNull() && !control.Text.IsEmpty()) { return control.SelectedDate.ToDateTime(); } return DObject.EMPTY_DATE_TIME; } /// /// 提取控件值 /// /// 控件 /// 值 public static int GetValue(this CheckBox control) { if (control.IsNull()) { return 0; } return control.Checked ? 1 : 0; } /// /// 提取控件值 /// /// 控件 public static string GetValue(this DropDownList control) { if (control.IsNull()) { return String.Empty; } //DHelper.SetSession(control.ID, control.SelectedValue); return control.SelectedValue; } /// /// 设置控件值 /// /// 控件 public static string GetValue(this CheckBoxList control) { string ret = String.Empty; if (control.IsNull()) { return ret; } for (int i = 0; i < control.Items.Count; i++) { if (control.Items[i].Selected) { ret += DString.COMMA + control.Items[i].Value.Trim(); } } if (ret.Length > 0) { ret = ret.Substring(1); } return ret; } /// /// 设置控件值 /// /// 控件 public static List GetIntValue(this CheckBoxList control) { List ret = new List(); if (control.IsNull()) { return ret; } for (int i = 0; i < control.Items.Count; i++) { if (control.Items[i].Selected) { ret.Add(control.Items[i].Value.Trim().ToInt32()); } } return ret; } /// /// 提取ICON图标地址 /// /// 图标 /// 图标地址 public static string GetIconUrl(this Icon icon) { if (!icon.IsNull()) { return IconHelper.GetIconUrl(icon); } return string.Empty; } #endregion #region 初始化 /// /// 创建树节点 /// /// 父编码 /// 节点 /// 数据 public static void CreateTree(string parent, TreeNode pnode, List featList) { foreach (ECustomTree feat in featList.FindAll(delegate(ECustomTree p) { return p.ParentId == parent; })) { TreeNode node = new TreeNode(); node.NodeID = feat.Id; // node.IconUrl = feat.PicD; node.Text = feat.Name; node.EnablePostBack = true; CreateTree(feat.Id, node, featList); //node.Expanded = true; pnode.Nodes.Add(node); } } /// /// 创建树节点 /// /// 父编码 /// 节点 /// 数据 /// 是否被选择 public static void CreateCheckTree(string parent, TreeNode pnode, List featList, Predicate isCheck) { foreach (ECustomTree feat in featList.FindAll(delegate(ECustomTree p) { return p.ParentId == parent; })) { TreeNode node = new TreeNode(); node.NodeID = feat.Id; //node.IconUrl = feat.PicD; node.Text = feat.Name; node.AutoPostBack = true; node.EnableCheckBox = true; if (isCheck(feat)) { node.Checked = true; } CreateCheckTree(feat.Id, node, featList, isCheck); node.Expanded = true; if (feat.IsTreeLeaf) { pnode.Nodes.Add(node); } } } public static List GetDataSource(string parent, List allList) { List parentList = new List(); int leave = 0; bool start = false;//是否禁用 foreach (ECustomTree featc in allList) { ECustomTree feat = new ECustomTree(); feat.TreeLevel = featc.TreeLevel; feat.Id = featc.Id; feat.Name = featc.Name; feat.IsTreeLeaf = featc.IsTreeLeaf; //开始禁用后 if (start) { //当前等级小于等于等级结束禁用 if (feat.TreeLevel <= leave) { start = false; } else { feat.Enabled = false; } } else { //当前等级等于等级开始禁用 if (parent.Equals(feat.Id) && !parent.IsEmpty()) { feat.Enabled = false; start = true; } leave = feat.TreeLevel; } parentList.Add(feat); } return parentList; } #endregion }