TextBox.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web.Mvc;
  6. namespace Bowin.Web.Controls.Mvc
  7. {
  8. public class TextBox : BaseFormControl
  9. {
  10. public TextBox()
  11. {
  12. TextBoxType = TextBoxType.Normal;
  13. Value = null;
  14. }
  15. public TextBoxType TextBoxType { get; set; }
  16. public string OnSelect { get; set; }
  17. public string OnChange { get; set; }
  18. public string OnClear { get; set; }
  19. public object Value { get; set; }
  20. public override string Render()
  21. {
  22. TagBuilder tabBuilder = new TagBuilder("input");
  23. tabBuilder.MergeAttributes<string, string>(GetPropertyList());
  24. if (this.Attributes != null)
  25. {
  26. tabBuilder.MergeAttributes<string, string>(this.Attributes);
  27. }
  28. //if (this.IsRequired.HasValue && this.IsRequired.Value)
  29. //{
  30. // if (this.Validator == null)
  31. // {
  32. // this.Validator = new ValidatorBox();
  33. // }
  34. // var validateOptions = this.Validator.GetOptions();
  35. // tabBuilder.AddCssClass(this.Validator.ValidatorClass);
  36. // //if (TextBoxType == Mvc.TextBoxType.Date || TextBoxType == Mvc.TextBoxType.DateTime)
  37. // //{
  38. // // validateOptions.Remove(Bowin.Web.Controls.Mvc.ValidatorBox.ValidatorBoxOptionKeys.REQUIRED);
  39. // // validateOptions.Add(Bowin.Web.Controls.Mvc.ValidatorBox.ValidatorBoxOptionKeys.REQUIRED, "false");
  40. // //}
  41. // tabBuilder.MergeAttributes(validateOptions);
  42. //}
  43. return tabBuilder.ToString(TagRenderMode.SelfClosing);
  44. }
  45. protected IDictionary<string, string> GetPropertyList()
  46. {
  47. var dictPropertyList = new Dictionary<string, string>();
  48. if (!string.IsNullOrEmpty(this.ID))
  49. dictPropertyList.Add("id", this.ID);
  50. if (!string.IsNullOrEmpty(this.Name))
  51. dictPropertyList.Add("name", this.Name);
  52. if(!string.IsNullOrEmpty(this.Title))
  53. dictPropertyList.Add("title",this.Title);
  54. var typeName = "text";
  55. switch (this.TextBoxType)
  56. {
  57. case TextBoxType.Password:
  58. typeName = "password";
  59. break;
  60. case TextBoxType.DateTime:
  61. break;
  62. case TextBoxType.Date:
  63. break;
  64. case TextBoxType.Normal:
  65. break;
  66. default:
  67. break;
  68. }
  69. dictPropertyList.Add("type", typeName);
  70. dictPropertyList.Add("value", TextBoxValueResolver.CreateValueResolver(this.TextBoxType).Execute(this));
  71. //需要采用my97calendar代替easyui的日期控件,原因是日期控件的验证和jquery.validate框架冲突
  72. //if (this.TextBoxType == Mvc.TextBoxType.DateTime)
  73. // dictPropertyList.Add("class", string.Format("easyui-datetimebox {0}", string.IsNullOrEmpty(this.CssClass) ? "" : this.CssClass));
  74. //else if (this.TextBoxType == Mvc.TextBoxType.Date)
  75. // dictPropertyList.Add("class", string.Format("easyui-datebox {0}", string.IsNullOrEmpty(this.CssClass) ? "" : this.CssClass));
  76. //else
  77. #region wdatepicker-options
  78. string optionString = "";
  79. var dictOptions = new Dictionary<string, string>();
  80. dictOptions.Add("autoUpdateOnChanged", "true");
  81. if (!string.IsNullOrEmpty(OnSelect) && (TextBoxType == TextBoxType.DateTime || TextBoxType == TextBoxType.Date || TextBoxType == TextBoxType.Time))
  82. {
  83. dictOptions.Add("onpicking", OnSelect);
  84. dictOptions.Add("onclearing", OnSelect);
  85. }
  86. if (!string.IsNullOrEmpty(OnClear))
  87. {
  88. if (TextBoxType == TextBoxType.Date || TextBoxType == TextBoxType.DateTime || TextBoxType == TextBoxType.Time)
  89. {
  90. dictOptions.Add("oncleared", OnClear);
  91. }
  92. }
  93. if (!string.IsNullOrEmpty(OnChange))
  94. {
  95. if (TextBoxType == TextBoxType.Date || TextBoxType == TextBoxType.DateTime)
  96. {
  97. dictOptions.Add("dchanged", OnChange);
  98. }
  99. else if (TextBoxType == TextBoxType.DateTime || TextBoxType == TextBoxType.Time)
  100. {
  101. dictOptions.Add("Hchanged", OnChange);
  102. dictOptions.Add("mchanged", OnChange);
  103. dictOptions.Add("schanged ", OnChange);
  104. }
  105. }
  106. if (dictOptions.Count > 0)
  107. optionString = string.Join(",", dictOptions.Select(x => string.Format("{0}:{1}", x.Key, x.Value))) + ",";
  108. #endregion wdatepicker-options
  109. dictPropertyList.Add("class", string.Format(" {0}", string.IsNullOrEmpty(this.CssClass) ? "" : this.CssClass));
  110. if (this.TextBoxType == Mvc.TextBoxType.DateTime)
  111. {
  112. dictPropertyList.Add("onclick", "WdatePicker({" + optionString + "dateFmt:'yyyy-MM-dd HH:mm:ss'})");
  113. }
  114. else if (this.TextBoxType == Mvc.TextBoxType.Date)
  115. {
  116. dictPropertyList.Add("onclick", "WdatePicker({" + optionString + "dateFmt:'yyyy-MM-dd'})");
  117. }
  118. else if (this.TextBoxType == Mvc.TextBoxType.Time)
  119. {
  120. dictPropertyList.Add("onclick", "WdatePicker({" + optionString + "dateFmt:'HH:mm:ss'})");
  121. }
  122. else if (this.TextBoxType == Mvc.TextBoxType.Month)
  123. {
  124. dictPropertyList.Add("onclick", "WdatePicker({" + optionString + "dateFmt:'yyyy-MM'})");
  125. }
  126. if (this.IsEnabled.HasValue && !this.IsEnabled.Value)
  127. dictPropertyList.Add("disabled", "true");
  128. if (this.IsShow.HasValue && !this.IsShow.Value)
  129. {
  130. if (dictPropertyList.Keys.Contains("style"))
  131. dictPropertyList["style"] += "display:none;";
  132. else
  133. dictPropertyList.Add("style", "display:none;");
  134. }
  135. //#region data-options
  136. //var dictOptions = new Dictionary<string, string>();
  137. //if (!string.IsNullOrEmpty(OnSelect) && (TextBoxType == TextBoxType.DateTime || TextBoxType == TextBoxType.Date))
  138. // dictOptions.Add("onSelect", OnSelect);
  139. //if (!string.IsNullOrEmpty(OnChange) && (TextBoxType == TextBoxType.DateTime || TextBoxType == TextBoxType.Date))
  140. //{
  141. // dictOptions.Add("parser", "function(s) { " + this.OnChange + "(); return $.fn.datebox.defaults.parser(s); }");
  142. //}
  143. //if (dictOptions.Count > 0)
  144. // dictPropertyList.Add("data-options", string.Join(",", dictOptions.Select(x => string.Format("{0}:{1}", x.Key, x.Value))));
  145. //#endregion data-options
  146. return dictPropertyList;
  147. }
  148. public static BaseControl CreateControl(TextBoxOptions textBoxOptions, IDictionary<string, string> attributes = null)
  149. {
  150. TextBox txtBox = new TextBox();
  151. txtBox.TextBoxType = textBoxOptions.TextBoxType;
  152. txtBox.CssClass = textBoxOptions.CssClass;
  153. txtBox.ID = textBoxOptions.ID ?? "txt_" + Guid.NewGuid().ToString().Substring(0, 6);
  154. txtBox.Name = textBoxOptions.Name;
  155. txtBox.Title = textBoxOptions.Title;
  156. txtBox.Value = textBoxOptions.Value;
  157. txtBox.IsEnabled = textBoxOptions.IsEnabled;
  158. txtBox.IsShow = textBoxOptions.IsShow;
  159. txtBox.IsRequired = textBoxOptions.IsRequired;
  160. txtBox.OnSelect = textBoxOptions.OnSelect;
  161. txtBox.OnChange = textBoxOptions.OnChange;
  162. txtBox.OnClear = textBoxOptions.OnClear;
  163. txtBox.Validator = textBoxOptions.Validator;
  164. if (attributes != null)
  165. {
  166. txtBox.Attributes = attributes;
  167. }
  168. return txtBox;
  169. }
  170. }
  171. }