DropdownList.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web.Mvc;
  6. using System.Configuration;
  7. namespace Bowin.Web.Controls.Mvc
  8. {
  9. public class DropdownList : BaseFormControl
  10. {
  11. public DropdownList()
  12. : base()
  13. {
  14. ItemList = new List<DropdownListItem>();
  15. SelectedValue = null;
  16. BindType = DropdownListBindType.None;
  17. }
  18. public int? Width { get; set; }
  19. public int? Height { get; set; }
  20. public bool? IsEditable { get; set; }
  21. public bool? IsMultiple { get; set; }
  22. public DropdownListBindType BindType { get; set; }
  23. public Dictionary<string, string> UrlParameters { get; set; }
  24. public static DropdownListItem SelectAllItem { get; set; }
  25. public static DropdownListItem PleaseSelectItem { get; set; }
  26. public static int SELECT_ALL { get; private set; }
  27. public static int PLEASE_SELECT { get; private set; }
  28. //public string Reload { get; set; }
  29. //public string ReloadSource { get; set; }
  30. static DropdownList()
  31. {
  32. SELECT_ALL = -1;
  33. PLEASE_SELECT = -1;
  34. SelectAllItem = new DropdownListItem { Text = "全部", Value = SELECT_ALL };
  35. PleaseSelectItem = new DropdownListItem { Text = "请选择", Value = PLEASE_SELECT };
  36. }
  37. public object SelectedValue
  38. {
  39. get;
  40. set;
  41. }
  42. public List<DropdownListItem> ItemList
  43. {
  44. get;
  45. private set;
  46. }
  47. public string ItemSourceUrl
  48. {
  49. get;
  50. set;
  51. }
  52. public string TextField
  53. {
  54. get;
  55. set;
  56. }
  57. public string ValueField
  58. {
  59. get;
  60. set;
  61. }
  62. /// <summary>
  63. /// 输出样式转换方法,如下
  64. /// function formatItem(row){
  65. /// var s = '<span style="font-weight:bold">' + row.text + '</span><br/>' +
  66. /// '<span style="color:#888">' + row.desc + '</span>';
  67. /// return s;
  68. ///}
  69. /// </summary>
  70. public string Formatter
  71. {
  72. get;
  73. set;
  74. }
  75. /// <summary>
  76. /// function(rec){
  77. /// var url = 'get_data2.php?id='+rec.id;
  78. /// $('#cc2').combobox('reload', url);
  79. /// }
  80. /// </summary>
  81. public string OnSelect
  82. {
  83. get;
  84. set;
  85. }
  86. public string OnUnselect { get; set; }
  87. public string OnChange { get; set; }
  88. public string OnLoadSuccess
  89. {
  90. get;
  91. set;
  92. }
  93. public string OnBeforeLoad { get; set; }
  94. protected IDictionary<string, string> GetPropertyList()
  95. {
  96. var dictPropertyList = new Dictionary<string, string>();
  97. if (!string.IsNullOrEmpty(this.ID))
  98. {
  99. dictPropertyList.Add("id", this.ID);
  100. }
  101. if (!string.IsNullOrEmpty(this.Name))
  102. {
  103. dictPropertyList.Add("name", this.Name);
  104. }
  105. if (this.IsShow.HasValue && !this.IsShow.Value)
  106. {
  107. if (dictPropertyList.Keys.Contains("style"))
  108. dictPropertyList["style"] += "display:none;";
  109. else
  110. dictPropertyList.Add("style", "display:none;");
  111. }
  112. if (this.Validator == null)
  113. {
  114. dictPropertyList.Add("class", string.Format("easyui-combobox {0}", string.IsNullOrEmpty(this.CssClass) ? "" : this.CssClass));
  115. }
  116. else
  117. {
  118. dictPropertyList.Add("class", string.Format("easyui-combobox {0} {1}", this.Validator.ValidatorClass,
  119. string.IsNullOrEmpty(this.CssClass) ? "" : this.CssClass));
  120. }
  121. //tabBuilder.MergeAttributes(this.Validator.GetOptions());
  122. //foreach (var attr in this.Attributes)
  123. //{
  124. // dictPropertyList.Add(attr.Key, attr.Value);
  125. //}
  126. #region data-options
  127. var dictOptions = new Dictionary<string, string>();
  128. if (!string.IsNullOrEmpty(this.ItemSourceUrl))
  129. {
  130. var url = "";
  131. var p = this.ItemSourceUrl.Split('?');
  132. if (p.Count() > 1)
  133. {
  134. url = p[0] + "?" + string.Join("&", new string[] { p[1], "bindType=" + (int)this.BindType });
  135. }
  136. else
  137. {
  138. url = this.ItemSourceUrl + "?bindType=" + (int)this.BindType;
  139. }
  140. if (this.UrlParameters != null && this.UrlParameters.Count > 0)
  141. {
  142. url +=
  143. string.Format(
  144. "{0}{1}",
  145. url.IndexOf("?") == -1 ? "?" : "&",
  146. string.Join("&", UrlParameters.Select(x => string.Format("{0}={1}", x.Key, x.Value)))
  147. );
  148. }
  149. dictOptions.Add("url", "'" + url + "'");
  150. if (string.IsNullOrEmpty(this.TextField))
  151. {
  152. throw new Exception("指定了ItemSourceUrl就一定要设置TextField");
  153. }
  154. if (string.IsNullOrEmpty(this.ValueField))
  155. {
  156. throw new Exception("指定了ItemSourceUrl就一定要设置ValueField");
  157. }
  158. }
  159. if (!string.IsNullOrEmpty(this.TextField))
  160. {
  161. dictOptions.Add("textField", "'" + this.TextField + "'");
  162. }
  163. if (!string.IsNullOrEmpty(this.ValueField))
  164. {
  165. dictOptions.Add("valueField", "'" + this.ValueField + "'");
  166. }
  167. //if (!string.IsNullOrEmpty(this.OnSelect))
  168. //{
  169. // dictOptions.Add("onSelect", this.OnSelect);
  170. //}
  171. dictOptions.Add("onSelect",
  172. "function(record){ BowinFunction.Combobox.OnSelect.call(this,record," +
  173. (string.IsNullOrEmpty(this.OnSelect) ?
  174. "null" :
  175. this.OnSelect.IndexOf("(") > 0 ?
  176. this.OnSelect.Substring(0, this.OnSelect.IndexOf("(")) :
  177. this.OnSelect
  178. ) + "); }");
  179. dictOptions.Add("onUnselect", "function(record){ BowinFunction.Combobox.OnUnselect.call(this,record," +
  180. (string.IsNullOrEmpty(this.OnUnselect) ?
  181. "null" :
  182. this.OnUnselect.IndexOf("(") > 0 ?
  183. this.OnUnselect.Substring(0, this.OnUnselect.IndexOf("(")) :
  184. this.OnUnselect
  185. ) + "); }");
  186. dictOptions.Add("onChange", "function(newValue,oldValue){ BowinFunction.Combobox.OnChange.call(this,newValue,oldValue," +
  187. (string.IsNullOrEmpty(this.OnChange) ?
  188. "null" :
  189. this.OnChange.IndexOf("(") > 0 ?
  190. this.OnChange.Substring(0, this.OnChange.IndexOf("(")) :
  191. this.OnChange
  192. ) + "); }");
  193. dictOptions.Add("onLoadSuccess", "function(){ BowinFunction.Combobox.OnLoadSuccess.call(this," +
  194. (string.IsNullOrEmpty(this.OnLoadSuccess) ?
  195. "null" :
  196. this.OnLoadSuccess.IndexOf("(") > 0 ?
  197. this.OnLoadSuccess.Substring(0, this.OnLoadSuccess.IndexOf("(")) :
  198. this.OnLoadSuccess
  199. ) + "); }");
  200. if (!string.IsNullOrEmpty(this.OnBeforeLoad))
  201. {
  202. dictOptions.Add("onBeforeLoad", this.OnBeforeLoad);
  203. }
  204. if (!string.IsNullOrEmpty(this.Formatter))
  205. {
  206. dictOptions.Add("formatter", this.Formatter);
  207. }
  208. if (this.Width.HasValue)
  209. {
  210. dictOptions.Add("width", this.Width.ToString());
  211. dictOptions.Add("panelWidth", this.Width.ToString());
  212. }
  213. //else
  214. //{
  215. // dictOptions.Add("panelWidth", "'auto'");
  216. //}
  217. if (this.Height.HasValue)
  218. {
  219. dictOptions.Add("panelHeight", this.Height.ToString());
  220. }
  221. else
  222. {
  223. dictOptions.Add("panelHeight", "'auto'");
  224. }
  225. if (string.IsNullOrEmpty(this.ItemSourceUrl))
  226. {
  227. if (this.BindType == DropdownListBindType.PleaseSelect)
  228. {
  229. ItemList.Insert(0, new DropdownListItem() { Text = "请选择", Value = PLEASE_SELECT });
  230. }
  231. else if (this.BindType == DropdownListBindType.SelectAll)
  232. {
  233. ItemList.Insert(0, new DropdownListItem() { Text = "全部", Value = SELECT_ALL });
  234. }
  235. }
  236. if (this.SelectedValue != null)
  237. {
  238. if (this.SelectedValue.GetType().BaseType != typeof(Array))
  239. dictOptions.Add("value", "'" + this.SelectedValue + "'");
  240. else
  241. {
  242. var valueArray = new List<string>();
  243. foreach (var item in ((Array)this.SelectedValue))
  244. {
  245. valueArray.Add(item.ToString());
  246. }
  247. dictOptions.Add("value", string.Concat("[", string.Join(",", valueArray.Select(x => string.Format("'{0}'", x.ToString()))), "]"));
  248. }
  249. }
  250. if (ItemList.Count > 0)
  251. {
  252. dictOptions.Add("data",
  253. string.Concat("[",
  254. string.Join(",", ItemList.Select(x => string.Format("{{ Text:'{0}',Value:'{1}' }}", x.Text, x.Value))),
  255. "]"));
  256. }
  257. if (IsEditable.HasValue)
  258. {
  259. dictOptions.Add("editable", this.IsEditable.Value.ToString().ToLower());
  260. }
  261. if (IsMultiple.HasValue)
  262. {
  263. dictOptions.Add("multiple", this.IsMultiple.Value.ToString().ToLower());
  264. }
  265. if (this.IsEnabled.HasValue)
  266. {
  267. dictOptions.Add("disabled", (!this.IsEnabled.Value).ToString().ToLower());
  268. }
  269. //dictOptions.Add("required", "true");
  270. dictPropertyList.Add("data-options", string.Join(",", dictOptions.Select(x => string.Format("{0}:{1}", x.Key, x.Value))));
  271. #endregion data-options
  272. //if (!string.IsNullOrEmpty(Reload))
  273. // dictPropertyList.Add("onchange", string.Format("JavaScript:BowinFunction.DropdownListReload(this,'{0}','{1}');", Reload, string.IsNullOrEmpty(ReloadSource) ? "" : ReloadSource));
  274. return dictPropertyList;
  275. }
  276. public override string Render()
  277. {
  278. TagBuilder tabBuilder = new TagBuilder("input");
  279. tabBuilder.MergeAttributes<string, string>(GetPropertyList());
  280. if (this.Validator != null)
  281. {
  282. tabBuilder.MergeAttributes(this.Validator.GetOptions());
  283. }
  284. if (this.Attributes != null)
  285. {
  286. tabBuilder.MergeAttributes<string, string>(this.Attributes);
  287. }
  288. if (this.IsRequired.HasValue && this.IsRequired.Value)
  289. {
  290. tabBuilder.AddCssClass(this.Validator.ValidatorClass);
  291. tabBuilder.MergeAttributes(this.Validator.GetOptions());
  292. }
  293. return tabBuilder.ToString(TagRenderMode.SelfClosing);
  294. //StringBuilder htmlBuilder = new StringBuilder();
  295. //htmlBuilder.Append("<input ");
  296. //htmlBuilder.Append(string.Join(" ", tabBuilder.Attributes.Select(x => string.Format("{0}=\"{1}\"", x.Key, x.Value))));
  297. //htmlBuilder.AppendLine(" />");
  298. //return htmlBuilder.ToString();
  299. }
  300. public static DropdownList CreateControl(DropdownListOptions dropdownListOptions, IDictionary<string, string> attributes = null)
  301. {
  302. DropdownList dropdownList = new DropdownList();
  303. dropdownList.BindType = dropdownListOptions.BindType;
  304. dropdownList.UrlParameters = dropdownListOptions.UrlParameters;
  305. dropdownList.CssClass = dropdownListOptions.CssClass;
  306. dropdownList.Formatter = dropdownListOptions.Formatter;
  307. dropdownList.Height = dropdownListOptions.Height;
  308. dropdownList.ID = dropdownListOptions.ID;
  309. if (dropdownListOptions.ItemList != null)
  310. {
  311. dropdownList.ItemList.AddRange(dropdownListOptions.ItemList);
  312. }
  313. dropdownList.ItemSourceUrl = dropdownListOptions.ItemSourceUrl;
  314. dropdownList.Name = dropdownListOptions.Name;
  315. dropdownList.OnSelect = dropdownListOptions.OnSelect;
  316. dropdownList.OnUnselect = dropdownListOptions.OnUnselect;
  317. dropdownList.OnChange = dropdownListOptions.OnChange;
  318. dropdownList.OnBeforeLoad = dropdownListOptions.OnBeforeLoad;
  319. dropdownList.SelectedValue = dropdownListOptions.SelectedValue;
  320. dropdownList.TextField = string.IsNullOrWhiteSpace(dropdownListOptions.TextField) ? "Text" : dropdownListOptions.TextField;
  321. dropdownList.ValueField = string.IsNullOrWhiteSpace(dropdownListOptions.ValueField) ? "Value" : dropdownListOptions.ValueField;
  322. dropdownList.Width = dropdownListOptions.Width;
  323. dropdownList.IsEditable = dropdownListOptions.IsEditable;
  324. dropdownList.IsMultiple = dropdownListOptions.IsMultiple;
  325. dropdownList.Attributes = attributes;
  326. dropdownList.Validator = dropdownListOptions.Validator;
  327. dropdownList.IsRequired = dropdownListOptions.IsRequired;
  328. dropdownList.IsEnabled = dropdownListOptions.IsEnabled;
  329. dropdownList.IsShow = dropdownListOptions.IsShow;
  330. dropdownList.OnLoadSuccess = dropdownListOptions.OnLoadSuccess;
  331. //dropdownList.Reload = dropdownListOptions.Reload;
  332. //dropdownList.ReloadSource = dropdownListOptions.ReloadSource;
  333. return dropdownList;
  334. }
  335. public static void FormatDropdownItemList(DropdownListBindType bindType, IList<DropdownListItem> items)
  336. {
  337. if (bindType == DropdownListBindType.PleaseSelect)
  338. {
  339. items.Insert(0, DropdownList.PleaseSelectItem);
  340. }
  341. else if (bindType == DropdownListBindType.SelectAll)
  342. {
  343. items.Insert(0, DropdownList.SelectAllItem);
  344. }
  345. }
  346. private static string ObjectToString(Array o)
  347. {
  348. return o.ToString();
  349. }
  350. private static string IntToString(int o)
  351. {
  352. return o.ToString();
  353. }
  354. }
  355. [Serializable]
  356. public class DropdownListItem
  357. {
  358. public DropdownListItem()
  359. {
  360. //IsSelected = false;
  361. }
  362. public string Text { get; set; }
  363. public object Value { get; set; }
  364. //public bool IsSelected { get; set; }
  365. }
  366. public enum DropdownListBindType
  367. {
  368. PleaseSelect,
  369. SelectAll,
  370. None
  371. }
  372. }