DExt.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using ETDExtUI;
  6. using ETD.Common;
  7. public static class DExt
  8. {
  9. #region 设置控件值
  10. /// <summary>
  11. /// 设置控件值
  12. /// </summary>
  13. /// <param name="control">控件</param>
  14. /// <param name="val">值</param>
  15. public static void SetValue(this Label control, string val)
  16. {
  17. if (!control.IsNull())
  18. {
  19. control.Text = val.IfNull().Trim();
  20. }
  21. }
  22. /// <summary>
  23. /// 设置控件值
  24. /// </summary>
  25. /// <param name="control">控件</param>
  26. /// <param name="val">值</param>
  27. public static void SetValue(this TextBox control, string val)
  28. {
  29. if (!control.IsNull())
  30. {
  31. control.Text = val.IfNull().Trim();
  32. }
  33. }
  34. /// <summary>
  35. /// 设置控件值
  36. /// </summary>
  37. /// <param name="control">控件</param>
  38. /// <param name="val">值</param>
  39. public static void SetValue(this NumberBox control, object val)
  40. {
  41. if (!control.IsNull())
  42. {
  43. control.Text = val.IfNull().Trim();
  44. }
  45. }
  46. /// <summary>
  47. /// 设置控件值
  48. /// </summary>
  49. /// <param name="control">控件</param>
  50. /// <param name="val">值</param>
  51. public static void SetValue(this TextArea control, string val)
  52. {
  53. if (!control.IsNull())
  54. {
  55. control.Text = val.IfNull().Trim();
  56. }
  57. }
  58. /// <summary>
  59. /// 设置控件值
  60. /// </summary>
  61. /// <param name="control">控件</param>
  62. /// <param name="val">值</param>
  63. public static void SetValue(this DatePicker control, object val)
  64. {
  65. if (!control.IsNull())
  66. {
  67. if (val is string)
  68. {
  69. control.Text = val.IfNull().Trim();
  70. return;
  71. }
  72. if (val is DateTime)
  73. {
  74. DateTime dt = val.ToDateTime();
  75. if (!dt.Equals(DObject.EMPTY_DATE_TIME))
  76. {
  77. control.SelectedDate = dt;
  78. }
  79. return;
  80. }
  81. }
  82. }
  83. /// <summary>
  84. /// 设置控件值
  85. /// </summary>
  86. /// <param name="control">控件</param>
  87. /// <param name="value">值</param>
  88. public static void SetValue(this CheckBox control, object val)
  89. {
  90. if (control.IsNull())
  91. {
  92. return;
  93. }
  94. if (val is bool)
  95. {
  96. control.Checked = (bool)val;
  97. return;
  98. }
  99. if (val.ToString().Equals(DString.NUM_1))
  100. {
  101. control.Checked = true;
  102. return;
  103. }
  104. control.Checked = false;
  105. }
  106. /// <summary>
  107. /// 设置控件值
  108. /// </summary>
  109. /// <param name="control">控件</param>
  110. /// <param name="value">值</param>
  111. public static void SetValue(this DropDownList control, string val)
  112. {
  113. if (!control.IsNull())
  114. {
  115. control.SelectedValue = val;
  116. }
  117. }
  118. /// <summary>
  119. /// 设置控件值
  120. /// </summary>
  121. /// <param name="control">控件</param>
  122. /// <param name="val">值</param>
  123. public static void SetValue(this ToolbarText control, string val)
  124. {
  125. if (!control.IsNull())
  126. {
  127. control.Text = val.IfNull().Trim();
  128. }
  129. }
  130. /// <summary>
  131. /// 设置控件值
  132. /// </summary>
  133. /// <param name="control">控件</param>
  134. /// <param name="val">值</param>
  135. public static void SetValue(this TwinTriggerBox control, string val)
  136. {
  137. if (!control.IsNull())
  138. {
  139. control.Text = val.IfNull().Trim();
  140. }
  141. }
  142. /// <summary>
  143. /// 设置点击
  144. /// </summary>
  145. /// <param name="control">控件</param>
  146. /// <param name="val">值</param>
  147. public static void SetClick(this Button control, string val)
  148. {
  149. if (!control.IsNull())
  150. {
  151. control.OnClientClick = val.IfNull().Trim();
  152. }
  153. }
  154. /// <summary>
  155. /// 绑定控件数据
  156. /// </summary>
  157. /// <param name="control">控件</param>
  158. /// <param name="value">数据</param>
  159. public static void SetDllDataSource<T>(this DropDownList control, List<T> data, string selectedvalue) where T : ECustomTree, new()
  160. {
  161. if (!control.IsNull())
  162. {
  163. control.EnableSimulateTree = true;
  164. control.DataValueField = DColumn.Id;
  165. control.DataTextField = DColumn.NameD;
  166. control.DataSimulateTreeLevelField = DColumn.LevelD;
  167. control.DataEnableSelectField = DColumn.StatusD;
  168. control.DataSource = data;
  169. control.DataBind();
  170. control.SetValue(selectedvalue);
  171. }
  172. }
  173. #endregion
  174. #region 提取控件值
  175. /// <summary>
  176. /// 提取控件值
  177. /// </summary>
  178. /// <param name="control">控件</param>
  179. /// <param name="val">值</param>
  180. public static string GetValue(this Label control)
  181. {
  182. if (!control.IsNull())
  183. {
  184. return control.Text.IfNull().Trim();
  185. }
  186. return String.Empty;
  187. }
  188. /// <summary>
  189. /// 提取控件值
  190. /// </summary>
  191. /// <param name="control">控件</param>
  192. /// <param name="val">值</param>
  193. public static string GetValue(this TextBox control)
  194. {
  195. if (!control.IsNull())
  196. {
  197. return control.Text.IfNull().FilterHTML().Trim();
  198. }
  199. return String.Empty;
  200. }
  201. /// <summary>
  202. /// 提取控件值
  203. /// </summary>
  204. /// <param name="control">控件</param>
  205. /// <param name="val">值</param>
  206. public static int GetValue(this NumberBox control)
  207. {
  208. if (!control.IsNull())
  209. {
  210. return control.Text.ToInt32();
  211. }
  212. return 0;
  213. }
  214. /// <summary>
  215. /// 提取控件值
  216. /// </summary>
  217. /// <param name="control">控件</param>
  218. /// <param name="val">值</param>
  219. public static decimal GetDecValue(this NumberBox control)
  220. {
  221. if (!control.IsNull())
  222. {
  223. return control.Text.ToDec();
  224. }
  225. return 0;
  226. }
  227. /// <summary>
  228. /// 提取控件值
  229. /// </summary>
  230. /// <param name="control">控件</param>
  231. /// <param name="val">值</param>
  232. public static string GetValue(this TextArea control)
  233. {
  234. if (!control.IsNull())
  235. {
  236. return control.Text.IfNull().FilterHTML().Trim();
  237. }
  238. return String.Empty;
  239. }
  240. /// <summary>
  241. /// 提取控件值
  242. /// </summary>
  243. /// <param name="control">控件</param>
  244. /// <param name="val">值</param>
  245. public static DateTime GetValue(this DatePicker control)
  246. {
  247. if (!control.IsNull())
  248. {
  249. return control.SelectedDate.ToDateTime();
  250. }
  251. return DObject.EMPTY_DATE_TIME;
  252. }
  253. /// <summary>
  254. /// 提取控件值为空时返回最小
  255. /// </summary>
  256. /// <param name="control">控件</param>
  257. /// <param name="val">值</param>
  258. public static DateTime GetValueEmptyMin(this DatePicker control)
  259. {
  260. if (!control.IsNull() && !control.Text.IsEmpty())
  261. {
  262. return control.SelectedDate.ToDateTime();
  263. }
  264. return DObject.EMPTY_DATE_TIME;
  265. }
  266. /// <summary>
  267. /// 提取控件值
  268. /// </summary>
  269. /// <param name="control">控件</param>
  270. /// <param name="val">值</param>
  271. public static int GetValue(this CheckBox control)
  272. {
  273. if (control.IsNull())
  274. {
  275. return 0;
  276. }
  277. return control.Checked ? 1 : 0;
  278. }
  279. /// <summary>
  280. /// 提取控件值
  281. /// </summary>
  282. /// <param name="control">控件</param>
  283. public static string GetValue(this DropDownList control)
  284. {
  285. if (control.IsNull())
  286. {
  287. return String.Empty;
  288. }
  289. //DHelper.SetSession(control.ID, control.SelectedValue);
  290. return control.SelectedValue;
  291. }
  292. /// <summary>
  293. /// 设置控件值
  294. /// </summary>
  295. /// <param name="control">控件</param>
  296. public static string GetValue(this CheckBoxList control)
  297. {
  298. string ret = String.Empty;
  299. if (control.IsNull())
  300. {
  301. return ret;
  302. }
  303. for (int i = 0; i < control.Items.Count; i++)
  304. {
  305. if (control.Items[i].Selected)
  306. {
  307. ret += DString.COMMA + control.Items[i].Value.Trim();
  308. }
  309. }
  310. if (ret.Length > 0)
  311. {
  312. ret = ret.Substring(1);
  313. }
  314. return ret;
  315. }
  316. /// <summary>
  317. /// 设置控件值
  318. /// </summary>
  319. /// <param name="control">控件</param>
  320. public static List<int> GetIntValue(this CheckBoxList control)
  321. {
  322. List<int> ret = new List<int>();
  323. if (control.IsNull())
  324. {
  325. return ret;
  326. }
  327. for (int i = 0; i < control.Items.Count; i++)
  328. {
  329. if (control.Items[i].Selected)
  330. {
  331. ret.Add(control.Items[i].Value.Trim().ToInt32());
  332. }
  333. }
  334. return ret;
  335. }
  336. /// <summary>
  337. /// 提取ICON图标地址
  338. /// </summary>
  339. /// <param name="icon">图标</param>
  340. /// <returns>图标地址</returns>
  341. public static string GetIconUrl(this Icon icon)
  342. {
  343. if (!icon.IsNull())
  344. {
  345. return IconHelper.GetIconUrl(icon);
  346. }
  347. return string.Empty;
  348. }
  349. #endregion
  350. #region 初始化
  351. /// <summary>
  352. /// 创建树节点
  353. /// </summary>
  354. /// <param name="parent">父编码</param>
  355. /// <param name="pnode">节点</param>
  356. /// <param name="featList">数据</param>
  357. public static void CreateTree(string parent, TreeNode pnode, List<ECustomTree> featList)
  358. {
  359. foreach (ECustomTree feat in featList.FindAll(delegate(ECustomTree p) { return p.ParentId == parent; }))
  360. {
  361. TreeNode node = new TreeNode();
  362. node.NodeID = feat.Id;
  363. // node.IconUrl = feat.PicD;
  364. node.Text = feat.Name;
  365. node.EnablePostBack = true;
  366. CreateTree(feat.Id, node, featList);
  367. //node.Expanded = true;
  368. pnode.Nodes.Add(node);
  369. }
  370. }
  371. /// <summary>
  372. /// 创建树节点
  373. /// </summary>
  374. /// <param name="parent">父编码</param>
  375. /// <param name="pnode">节点</param>
  376. /// <param name="featList">数据</param>
  377. /// <param name="featList">是否被选择</param>
  378. public static void CreateCheckTree(string parent, TreeNode pnode, List<ECustomTree> featList, Predicate<ECustomTree> isCheck)
  379. {
  380. foreach (ECustomTree feat in featList.FindAll(delegate(ECustomTree p) { return p.ParentId == parent; }))
  381. {
  382. TreeNode node = new TreeNode();
  383. node.NodeID = feat.Id;
  384. //node.IconUrl = feat.PicD;
  385. node.Text = feat.Name;
  386. node.AutoPostBack = true;
  387. node.EnableCheckBox = true;
  388. if (isCheck(feat))
  389. {
  390. node.Checked = true;
  391. }
  392. CreateCheckTree(feat.Id, node, featList, isCheck);
  393. node.Expanded = true;
  394. if (feat.IsTreeLeaf)
  395. {
  396. pnode.Nodes.Add(node);
  397. }
  398. }
  399. }
  400. public static List<ECustomTree> GetDataSource(string parent, List<ECustomTree> allList)
  401. {
  402. List<ECustomTree> parentList = new List<ECustomTree>();
  403. int leave = 0;
  404. bool start = false;//是否禁用
  405. foreach (ECustomTree featc in allList)
  406. {
  407. ECustomTree feat = new ECustomTree();
  408. feat.TreeLevel = featc.TreeLevel;
  409. feat.Id = featc.Id;
  410. feat.Name = featc.Name;
  411. feat.IsTreeLeaf = featc.IsTreeLeaf;
  412. //开始禁用后
  413. if (start)
  414. {
  415. //当前等级小于等于等级结束禁用
  416. if (feat.TreeLevel <= leave)
  417. {
  418. start = false;
  419. }
  420. else
  421. {
  422. feat.Enabled = false;
  423. }
  424. }
  425. else
  426. {
  427. //当前等级等于等级开始禁用
  428. if (parent.Equals(feat.Id) && !parent.IsEmpty())
  429. {
  430. feat.Enabled = false;
  431. start = true;
  432. }
  433. leave = feat.TreeLevel;
  434. }
  435. parentList.Add(feat);
  436. }
  437. return parentList;
  438. }
  439. #endregion
  440. }