using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Mvc; namespace Bowin.Web.Controls.Mvc { public class TextBox : BaseFormControl { public TextBox() { TextBoxType = TextBoxType.Normal; Value = null; } public TextBoxType TextBoxType { get; set; } public string OnSelect { get; set; } public string OnChange { get; set; } public string OnClear { get; set; } public object Value { get; set; } public override string Render() { TagBuilder tabBuilder = new TagBuilder("input"); tabBuilder.MergeAttributes(GetPropertyList()); if (this.Attributes != null) { tabBuilder.MergeAttributes(this.Attributes); } //if (this.IsRequired.HasValue && this.IsRequired.Value) //{ // if (this.Validator == null) // { // this.Validator = new ValidatorBox(); // } // var validateOptions = this.Validator.GetOptions(); // tabBuilder.AddCssClass(this.Validator.ValidatorClass); // //if (TextBoxType == Mvc.TextBoxType.Date || TextBoxType == Mvc.TextBoxType.DateTime) // //{ // // validateOptions.Remove(Bowin.Web.Controls.Mvc.ValidatorBox.ValidatorBoxOptionKeys.REQUIRED); // // validateOptions.Add(Bowin.Web.Controls.Mvc.ValidatorBox.ValidatorBoxOptionKeys.REQUIRED, "false"); // //} // tabBuilder.MergeAttributes(validateOptions); //} return tabBuilder.ToString(TagRenderMode.SelfClosing); } protected IDictionary GetPropertyList() { var dictPropertyList = new Dictionary(); if (!string.IsNullOrEmpty(this.ID)) dictPropertyList.Add("id", this.ID); if (!string.IsNullOrEmpty(this.Name)) dictPropertyList.Add("name", this.Name); if(!string.IsNullOrEmpty(this.Title)) dictPropertyList.Add("title",this.Title); var typeName = "text"; switch (this.TextBoxType) { case TextBoxType.Password: typeName = "password"; break; case TextBoxType.DateTime: break; case TextBoxType.Date: break; case TextBoxType.Normal: break; default: break; } dictPropertyList.Add("type", typeName); dictPropertyList.Add("value", TextBoxValueResolver.CreateValueResolver(this.TextBoxType).Execute(this)); //需要采用my97calendar代替easyui的日期控件,原因是日期控件的验证和jquery.validate框架冲突 //if (this.TextBoxType == Mvc.TextBoxType.DateTime) // dictPropertyList.Add("class", string.Format("easyui-datetimebox {0}", string.IsNullOrEmpty(this.CssClass) ? "" : this.CssClass)); //else if (this.TextBoxType == Mvc.TextBoxType.Date) // dictPropertyList.Add("class", string.Format("easyui-datebox {0}", string.IsNullOrEmpty(this.CssClass) ? "" : this.CssClass)); //else #region wdatepicker-options string optionString = ""; var dictOptions = new Dictionary(); dictOptions.Add("autoUpdateOnChanged", "true"); if (!string.IsNullOrEmpty(OnSelect) && (TextBoxType == TextBoxType.DateTime || TextBoxType == TextBoxType.Date || TextBoxType == TextBoxType.Time)) { dictOptions.Add("onpicking", OnSelect); dictOptions.Add("onclearing", OnSelect); } if (!string.IsNullOrEmpty(OnClear)) { if (TextBoxType == TextBoxType.Date || TextBoxType == TextBoxType.DateTime || TextBoxType == TextBoxType.Time) { dictOptions.Add("oncleared", OnClear); } } if (!string.IsNullOrEmpty(OnChange)) { if (TextBoxType == TextBoxType.Date || TextBoxType == TextBoxType.DateTime) { dictOptions.Add("dchanged", OnChange); } else if (TextBoxType == TextBoxType.DateTime || TextBoxType == TextBoxType.Time) { dictOptions.Add("Hchanged", OnChange); dictOptions.Add("mchanged", OnChange); dictOptions.Add("schanged ", OnChange); } } if (dictOptions.Count > 0) optionString = string.Join(",", dictOptions.Select(x => string.Format("{0}:{1}", x.Key, x.Value))) + ","; #endregion wdatepicker-options dictPropertyList.Add("class", string.Format(" {0}", string.IsNullOrEmpty(this.CssClass) ? "" : this.CssClass)); if (this.TextBoxType == Mvc.TextBoxType.DateTime) { dictPropertyList.Add("onclick", "WdatePicker({" + optionString + "dateFmt:'yyyy-MM-dd HH:mm:ss'})"); } else if (this.TextBoxType == Mvc.TextBoxType.Date) { dictPropertyList.Add("onclick", "WdatePicker({" + optionString + "dateFmt:'yyyy-MM-dd'})"); } else if (this.TextBoxType == Mvc.TextBoxType.Time) { dictPropertyList.Add("onclick", "WdatePicker({" + optionString + "dateFmt:'HH:mm:ss'})"); } else if (this.TextBoxType == Mvc.TextBoxType.Month) { dictPropertyList.Add("onclick", "WdatePicker({" + optionString + "dateFmt:'yyyy-MM'})"); } if (this.IsEnabled.HasValue && !this.IsEnabled.Value) dictPropertyList.Add("disabled", "true"); if (this.IsShow.HasValue && !this.IsShow.Value) { if (dictPropertyList.Keys.Contains("style")) dictPropertyList["style"] += "display:none;"; else dictPropertyList.Add("style", "display:none;"); } //#region data-options //var dictOptions = new Dictionary(); //if (!string.IsNullOrEmpty(OnSelect) && (TextBoxType == TextBoxType.DateTime || TextBoxType == TextBoxType.Date)) // dictOptions.Add("onSelect", OnSelect); //if (!string.IsNullOrEmpty(OnChange) && (TextBoxType == TextBoxType.DateTime || TextBoxType == TextBoxType.Date)) //{ // dictOptions.Add("parser", "function(s) { " + this.OnChange + "(); return $.fn.datebox.defaults.parser(s); }"); //} //if (dictOptions.Count > 0) // dictPropertyList.Add("data-options", string.Join(",", dictOptions.Select(x => string.Format("{0}:{1}", x.Key, x.Value)))); //#endregion data-options return dictPropertyList; } public static BaseControl CreateControl(TextBoxOptions textBoxOptions, IDictionary attributes = null) { TextBox txtBox = new TextBox(); txtBox.TextBoxType = textBoxOptions.TextBoxType; txtBox.CssClass = textBoxOptions.CssClass; txtBox.ID = textBoxOptions.ID ?? "txt_" + Guid.NewGuid().ToString().Substring(0, 6); txtBox.Name = textBoxOptions.Name; txtBox.Title = textBoxOptions.Title; txtBox.Value = textBoxOptions.Value; txtBox.IsEnabled = textBoxOptions.IsEnabled; txtBox.IsShow = textBoxOptions.IsShow; txtBox.IsRequired = textBoxOptions.IsRequired; txtBox.OnSelect = textBoxOptions.OnSelect; txtBox.OnChange = textBoxOptions.OnChange; txtBox.OnClear = textBoxOptions.OnClear; txtBox.Validator = textBoxOptions.Validator; if (attributes != null) { txtBox.Attributes = attributes; } return txtBox; } } }