using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Mvc; using System.Configuration; namespace Bowin.Web.Controls.Mvc { public class DropdownList : BaseFormControl { public DropdownList() : base() { ItemList = new List(); SelectedValue = null; BindType = DropdownListBindType.None; } public int? Width { get; set; } public int? Height { get; set; } public bool? IsEditable { get; set; } public bool? IsMultiple { get; set; } public DropdownListBindType BindType { get; set; } public Dictionary UrlParameters { get; set; } public static DropdownListItem SelectAllItem { get; set; } public static DropdownListItem PleaseSelectItem { get; set; } public static int SELECT_ALL { get; private set; } public static int PLEASE_SELECT { get; private set; } //public string Reload { get; set; } //public string ReloadSource { get; set; } static DropdownList() { SELECT_ALL = -1; PLEASE_SELECT = -1; SelectAllItem = new DropdownListItem { Text = "全部", Value = SELECT_ALL }; PleaseSelectItem = new DropdownListItem { Text = "请选择", Value = PLEASE_SELECT }; } public object SelectedValue { get; set; } public List ItemList { get; private set; } public string ItemSourceUrl { get; set; } public string TextField { get; set; } public string ValueField { get; set; } /// /// 输出样式转换方法,如下 /// function formatItem(row){ /// var s = '' + row.text + '
' + /// '' + row.desc + ''; /// return s; ///} ///
public string Formatter { get; set; } /// /// function(rec){ /// var url = 'get_data2.php?id='+rec.id; /// $('#cc2').combobox('reload', url); /// } /// public string OnSelect { get; set; } public string OnUnselect { get; set; } public string OnChange { get; set; } public string OnLoadSuccess { get; set; } public string OnBeforeLoad { get; set; } 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 (this.IsShow.HasValue && !this.IsShow.Value) { if (dictPropertyList.Keys.Contains("style")) dictPropertyList["style"] += "display:none;"; else dictPropertyList.Add("style", "display:none;"); } if (this.Validator == null) { dictPropertyList.Add("class", string.Format("easyui-combobox {0}", string.IsNullOrEmpty(this.CssClass) ? "" : this.CssClass)); } else { dictPropertyList.Add("class", string.Format("easyui-combobox {0} {1}", this.Validator.ValidatorClass, string.IsNullOrEmpty(this.CssClass) ? "" : this.CssClass)); } //tabBuilder.MergeAttributes(this.Validator.GetOptions()); //foreach (var attr in this.Attributes) //{ // dictPropertyList.Add(attr.Key, attr.Value); //} #region data-options var dictOptions = new Dictionary(); if (!string.IsNullOrEmpty(this.ItemSourceUrl)) { var url = ""; var p = this.ItemSourceUrl.Split('?'); if (p.Count() > 1) { url = p[0] + "?" + string.Join("&", new string[] { p[1], "bindType=" + (int)this.BindType }); } else { url = this.ItemSourceUrl + "?bindType=" + (int)this.BindType; } if (this.UrlParameters != null && this.UrlParameters.Count > 0) { url += string.Format( "{0}{1}", url.IndexOf("?") == -1 ? "?" : "&", string.Join("&", UrlParameters.Select(x => string.Format("{0}={1}", x.Key, x.Value))) ); } dictOptions.Add("url", "'" + url + "'"); if (string.IsNullOrEmpty(this.TextField)) { throw new Exception("指定了ItemSourceUrl就一定要设置TextField"); } if (string.IsNullOrEmpty(this.ValueField)) { throw new Exception("指定了ItemSourceUrl就一定要设置ValueField"); } } if (!string.IsNullOrEmpty(this.TextField)) { dictOptions.Add("textField", "'" + this.TextField + "'"); } if (!string.IsNullOrEmpty(this.ValueField)) { dictOptions.Add("valueField", "'" + this.ValueField + "'"); } //if (!string.IsNullOrEmpty(this.OnSelect)) //{ // dictOptions.Add("onSelect", this.OnSelect); //} dictOptions.Add("onSelect", "function(record){ BowinFunction.Combobox.OnSelect.call(this,record," + (string.IsNullOrEmpty(this.OnSelect) ? "null" : this.OnSelect.IndexOf("(") > 0 ? this.OnSelect.Substring(0, this.OnSelect.IndexOf("(")) : this.OnSelect ) + "); }"); dictOptions.Add("onUnselect", "function(record){ BowinFunction.Combobox.OnUnselect.call(this,record," + (string.IsNullOrEmpty(this.OnUnselect) ? "null" : this.OnUnselect.IndexOf("(") > 0 ? this.OnUnselect.Substring(0, this.OnUnselect.IndexOf("(")) : this.OnUnselect ) + "); }"); dictOptions.Add("onChange", "function(newValue,oldValue){ BowinFunction.Combobox.OnChange.call(this,newValue,oldValue," + (string.IsNullOrEmpty(this.OnChange) ? "null" : this.OnChange.IndexOf("(") > 0 ? this.OnChange.Substring(0, this.OnChange.IndexOf("(")) : this.OnChange ) + "); }"); dictOptions.Add("onLoadSuccess", "function(){ BowinFunction.Combobox.OnLoadSuccess.call(this," + (string.IsNullOrEmpty(this.OnLoadSuccess) ? "null" : this.OnLoadSuccess.IndexOf("(") > 0 ? this.OnLoadSuccess.Substring(0, this.OnLoadSuccess.IndexOf("(")) : this.OnLoadSuccess ) + "); }"); if (!string.IsNullOrEmpty(this.OnBeforeLoad)) { dictOptions.Add("onBeforeLoad", this.OnBeforeLoad); } if (!string.IsNullOrEmpty(this.Formatter)) { dictOptions.Add("formatter", this.Formatter); } if (this.Width.HasValue) { dictOptions.Add("width", this.Width.ToString()); dictOptions.Add("panelWidth", this.Width.ToString()); } //else //{ // dictOptions.Add("panelWidth", "'auto'"); //} if (this.Height.HasValue) { dictOptions.Add("panelHeight", this.Height.ToString()); } else { dictOptions.Add("panelHeight", "'auto'"); } if (string.IsNullOrEmpty(this.ItemSourceUrl)) { if (this.BindType == DropdownListBindType.PleaseSelect) { ItemList.Insert(0, new DropdownListItem() { Text = "请选择", Value = PLEASE_SELECT }); } else if (this.BindType == DropdownListBindType.SelectAll) { ItemList.Insert(0, new DropdownListItem() { Text = "全部", Value = SELECT_ALL }); } } if (this.SelectedValue != null) { if (this.SelectedValue.GetType().BaseType != typeof(Array)) dictOptions.Add("value", "'" + this.SelectedValue + "'"); else { var valueArray = new List(); foreach (var item in ((Array)this.SelectedValue)) { valueArray.Add(item.ToString()); } dictOptions.Add("value", string.Concat("[", string.Join(",", valueArray.Select(x => string.Format("'{0}'", x.ToString()))), "]")); } } if (ItemList.Count > 0) { dictOptions.Add("data", string.Concat("[", string.Join(",", ItemList.Select(x => string.Format("{{ Text:'{0}',Value:'{1}' }}", x.Text, x.Value))), "]")); } if (IsEditable.HasValue) { dictOptions.Add("editable", this.IsEditable.Value.ToString().ToLower()); } if (IsMultiple.HasValue) { dictOptions.Add("multiple", this.IsMultiple.Value.ToString().ToLower()); } if (this.IsEnabled.HasValue) { dictOptions.Add("disabled", (!this.IsEnabled.Value).ToString().ToLower()); } //dictOptions.Add("required", "true"); dictPropertyList.Add("data-options", string.Join(",", dictOptions.Select(x => string.Format("{0}:{1}", x.Key, x.Value)))); #endregion data-options //if (!string.IsNullOrEmpty(Reload)) // dictPropertyList.Add("onchange", string.Format("JavaScript:BowinFunction.DropdownListReload(this,'{0}','{1}');", Reload, string.IsNullOrEmpty(ReloadSource) ? "" : ReloadSource)); return dictPropertyList; } public override string Render() { TagBuilder tabBuilder = new TagBuilder("input"); tabBuilder.MergeAttributes(GetPropertyList()); if (this.Validator != null) { tabBuilder.MergeAttributes(this.Validator.GetOptions()); } if (this.Attributes != null) { tabBuilder.MergeAttributes(this.Attributes); } if (this.IsRequired.HasValue && this.IsRequired.Value) { tabBuilder.AddCssClass(this.Validator.ValidatorClass); tabBuilder.MergeAttributes(this.Validator.GetOptions()); } return tabBuilder.ToString(TagRenderMode.SelfClosing); //StringBuilder htmlBuilder = new StringBuilder(); //htmlBuilder.Append(" string.Format("{0}=\"{1}\"", x.Key, x.Value)))); //htmlBuilder.AppendLine(" />"); //return htmlBuilder.ToString(); } public static DropdownList CreateControl(DropdownListOptions dropdownListOptions, IDictionary attributes = null) { DropdownList dropdownList = new DropdownList(); dropdownList.BindType = dropdownListOptions.BindType; dropdownList.UrlParameters = dropdownListOptions.UrlParameters; dropdownList.CssClass = dropdownListOptions.CssClass; dropdownList.Formatter = dropdownListOptions.Formatter; dropdownList.Height = dropdownListOptions.Height; dropdownList.ID = dropdownListOptions.ID; if (dropdownListOptions.ItemList != null) { dropdownList.ItemList.AddRange(dropdownListOptions.ItemList); } dropdownList.ItemSourceUrl = dropdownListOptions.ItemSourceUrl; dropdownList.Name = dropdownListOptions.Name; dropdownList.OnSelect = dropdownListOptions.OnSelect; dropdownList.OnUnselect = dropdownListOptions.OnUnselect; dropdownList.OnChange = dropdownListOptions.OnChange; dropdownList.OnBeforeLoad = dropdownListOptions.OnBeforeLoad; dropdownList.SelectedValue = dropdownListOptions.SelectedValue; dropdownList.TextField = string.IsNullOrWhiteSpace(dropdownListOptions.TextField) ? "Text" : dropdownListOptions.TextField; dropdownList.ValueField = string.IsNullOrWhiteSpace(dropdownListOptions.ValueField) ? "Value" : dropdownListOptions.ValueField; dropdownList.Width = dropdownListOptions.Width; dropdownList.IsEditable = dropdownListOptions.IsEditable; dropdownList.IsMultiple = dropdownListOptions.IsMultiple; dropdownList.Attributes = attributes; dropdownList.Validator = dropdownListOptions.Validator; dropdownList.IsRequired = dropdownListOptions.IsRequired; dropdownList.IsEnabled = dropdownListOptions.IsEnabled; dropdownList.IsShow = dropdownListOptions.IsShow; dropdownList.OnLoadSuccess = dropdownListOptions.OnLoadSuccess; //dropdownList.Reload = dropdownListOptions.Reload; //dropdownList.ReloadSource = dropdownListOptions.ReloadSource; return dropdownList; } public static void FormatDropdownItemList(DropdownListBindType bindType, IList items) { if (bindType == DropdownListBindType.PleaseSelect) { items.Insert(0, DropdownList.PleaseSelectItem); } else if (bindType == DropdownListBindType.SelectAll) { items.Insert(0, DropdownList.SelectAllItem); } } private static string ObjectToString(Array o) { return o.ToString(); } private static string IntToString(int o) { return o.ToString(); } } [Serializable] public class DropdownListItem { public DropdownListItem() { //IsSelected = false; } public string Text { get; set; } public object Value { get; set; } //public bool IsSelected { get; set; } } public enum DropdownListBindType { PleaseSelect, SelectAll, None } }