/// <summary> /// 选择项 /// </summary> /// <param name="control">ListControl控件</param> /// <param name="selectedValue">选择项</param> public static void SelectedItems(this ListControl control, object selectedValue) { var selValue = string.Empty; if (selectedValue is Enum) { selValue = Convert.ToString((int)selectedValue); } else if (selectedValue is bool) { selValue = selectedValue.ToString().ToLower(); } else { selValue = selectedValue == null ? string.Empty : selectedValue.ToString(); } // 清除当前选择的项 control.ClearSelection(); if (control.Items.FindByValue(selValue) != null) { control.Items.FindByValue(selValue).Selected = true; } else if (control.Items.FindByText(selValue) != null) { control.Items.FindByText(selValue).Selected = true; } }
public static void SetValue(System.Web.UI.WebControls.ListControl myListControl, object _Value) { myListControl.ClearSelection(); if (myListControl.Items.Count == 1) { myListControl.SelectedIndex = 0; } else { if (_Value.ToString() == "0") { myListControl.SelectedIndex = 0; } else { foreach (ListItem _item in myListControl.Items) { if (_item.Value.ToString().ToLower() == _Value.ToString().ToLower()) { _item.Selected = true; break; } } } } }
public static void SetValue(System.Web.UI.WebControls.ListControl myListControl, object objectValue) { try { myListControl.ClearSelection(); if (myListControl.Items.Count == 0) { return; } if (objectValue != null) { //myListControl.Items.FindByValue(objectValue.ToString()).Selected= true ; foreach (ListItem item in myListControl.Items) { if (item.Value.ToString().ToLower() == objectValue.ToString().ToLower()) { item.Selected = true; break; } } } } catch { myListControl.SelectedIndex = 0; //var error = ex.Message; //throw new Exception(error); } }
public static void SafeSelect(ListControl ddl, string val) { ListItem li = ddl.Items.FindByValue(val); if (li != null) { ddl.ClearSelection(); li.Selected = true; } }
public static bool TrySelect(ListControl list, string text, string value, ref ListItem li) { li = null; if (string.IsNullOrEmpty(value)) return false; li = list.Items.FindByValue(value); if (li != null) { list.ClearSelection(); li.Selected = true; } return (li != null); }
public static ListItem SelectAdd(ListControl list, string text, string value) { ListItem li = list.Items.FindByValue(value); if (li == null) { li = new ListItem(text, value); list.Items.Add(li); } list.ClearSelection(); li.Selected = true; return li; }
public static void SetValue(System.Web.UI.WebControls.ListControl myListControl, object objectValue) { try { myListControl.ClearSelection(); if (myListControl.Items.Count > 0) { if (myListControl.Items.Count == 1) { myListControl.SelectedIndex = 0; } else { if (objectValue != null) { if (objectValue.ToString() == "0") { myListControl.SelectedIndex = 0; } else { foreach (ListItem item in myListControl.Items) { if (item.Value.ToString().ToLower() == objectValue.ToString().ToLower()) { item.Selected = true; break; } } } } } } } catch (Exception ex) { if (myListControl.Items.Count > 0) { myListControl.SelectedIndex = 0; } else { var error = ex.Message; throw new Exception(error); } } }
/// <summary> /// Selects the item in the list control that contains the specified value, if it exists. /// </summary> /// <param name="dropDownList"></param> /// <param name="selectedValue">The value of the item in the list control to select</param> /// <returns>Returns true if the value exists in the list control, false otherwise</returns> public static bool SetSelectedValue(ListControl dropDownList, String selectedValue) { if (dropDownList != null) { dropDownList.ClearSelection(); ListItem selectedListItem = dropDownList.Items.FindByValue(selectedValue); if (selectedListItem != null) { selectedListItem.Selected = true; return true; } } return false; }
/// <summary> /// string[]绑定到WebControl /// </summary> /// <param name="control">要绑定的ddl</param> /// <param name="trueCaption">值为是的提示</param> /// <param name="falseCaption">值为不是的提示</param> /// <param name="NoSelectCaption">未选择的提示</param> public static void Bind(this ListControl control, string trueCaption = "是", string falseCaption = "否", string NoSelectCaption = "") { if (!NoSelectCaption.IsNullOrEmpty()) { control.Items.Add(new ListItem { Value = "", Text = NoSelectCaption }); } control.Items.Add(new ListItem { Value = "false", Text = falseCaption }); control.Items.Add(new ListItem { Value = "true", Text = trueCaption }); // 清除当前选择的项 control.ClearSelection(); control.SelectedIndex = 0; }
public static void SetValueMultiple(System.Web.UI.WebControls.ListControl myListControl, string value) { try { if (myListControl.Items.Count > 0) { if (value != null) { myListControl.ClearSelection(); foreach (ListItem item in myListControl.Items) { if (value.IndexOf(item.Value) != -1) { item.Selected = true; } } } } } catch (Exception ex) { } }
/// <summary>列表框</summary> /// <param name="entity"></param> /// <param name="field"></param> /// <param name="control"></param> /// <param name="canSave"></param> protected virtual void SetFormItemListControl(IEntity entity, FieldItem field, ListControl control, Boolean canSave) { if (control.Items.Count < 1 || !String.IsNullOrEmpty(control.DataSourceID)) { control.DataBind(); // 这个赋值会影响RequiresDataBinding,而RequiresDataBinding会导致列表控件在OnPreRender阶段重新绑定,造成当前设定的值丢失。 //control.AppendDataBoundItems = false; } if (control.Items.Count < 1) return; String value = String.Empty + entity[field.Name]; try { control.SelectedValue = value; if (control.GetType() == typeof(DropDownList) || control.GetType() == typeof(ListControl)) { /* * 对于ListControl和DropDownList,仅PostBack时有可能抛出异常,初次打开有可能在PerformDataBinding中抛出异常,所以要做额外检测 * * 因为DropDownList.SelectedIndex get时有可能修改Items[0].Selected, 所以下面代码最好避免访问SelectedIndex,SelectedValue * * 对于XControl.DropDownList始终没问题 * */ var selected = false; for (int i = 0; i < control.Items.Count; i++) { var item = control.Items[i]; if (item.Selected) { selected = item.Value == value; break; } } if (!selected) { // 没有任何选中项或选中项不是设置的值 throw new ArgumentException(); } } } catch (ArgumentException) { var li = control.Items.FindByValue(value); if (li == null) { li = new ListItem(value, value); control.Items.Add(li); } control.ClearSelection(); li.Selected = true; } }
/// <summary> /// Tenta selecionar o valor especificado no controle de lista. /// </summary> /// <param name="ctl">O <see cref="ListControl"/></param> /// <param name="val">O valor.</param> /// <param name="throwException">Se verdadeiro, uma exceção é lançada, ao invés de simplesmente returnar <c>false</c>.</param> /// <returns>Verdadeiro se o valor pôde ser atribuído, falso caso contrário</returns> /// <exception cref="IndexOutOfRangeException">Se o valor não for encontrado no <see cref="ListControl"/> especificado em <paramref name="ctl"/>.</exception> public static bool TrySetValue(ListControl ctl, string val, bool throwException) { ListItem li = ctl.Items.FindByValue(val); if (li == null) { if (throwException) throw new IndexOutOfRangeException(String.Format("O valor '{0}' não foi encontrado no {1} '{2}'.", val, ctl.GetType().Name, ctl.ID)); else if (!IsSingleSelectionControl(ctl)) ctl.ClearSelection(); } else { if (IsSingleSelectionControl(ctl)) ctl.SelectedValue = li.Value; else li.Selected = true; } return li != null; }
/// <summary> /// 设置ListControl选中项 /// </summary> /// <param name="listControl">ListControl控件</param> /// <param name="selectValue">选中的Value</param> public static void SetSelectedByValue(ListControl listControl, string selectValue) { listControl.ClearSelection(); ListItem li = listControl.Items.FindByValue(selectValue); if (li != null) { li.Selected = true; } }
public static void SelectListItemByValue(ListControl dropDownList, object value, object defaultValue) { if (dropDownList == null) return; dropDownList.ClearSelection(); var selectedItem = dropDownList.Items.FindByValue(value == null ? (defaultValue == null ? string.Empty : defaultValue.ToString()) : value.ToString()); if (selectedItem != null) selectedItem.Selected = true; }
/// <summary> /// 设置ListControl选中项 /// </summary> /// <param name="listControl">ListControl控件</param> /// <param name="selectValue">选中的Value</param> /// <param name="separator">分隔符</param> public static void SetMultSelectedByValue(ListControl listControl, string selectValue, string separator) { listControl.ClearSelection(); string[] vals = selectValue.Split(new string[]{ separator}, StringSplitOptions.RemoveEmptyEntries); foreach (ListItem li in listControl.Items) { li.Selected = vals.Contains(li.Value); } }
public static bool SelectedByValue(ListControl lst, string strValue) { ListItem item = lst.Items.FindByValue(strValue); if (item == null) { return false; } if ((lst.GetType().ToString() != "System.Web.UI.WebControls.ListBox") && (lst.GetType().ToString() != "System.Web.UI.WebControls.CheckBoxList")) { lst.ClearSelection(); } item.Selected = true; return true; }
/// <summary> /// ��ȫ����<see cref="ListControl"/>��ֵ /// </summary> /// <param name="listCtrl">��Ҫ���õ� see cref="ListControl"/></param> /// <param name="value">���õ�ֵ</param> /// <param name="text">���õ�ֵ��</param> /// <param name="addIfNotExists">����������Ƿ��������</param> /// <param name="addEmptyItem">�Ƿ���ӿ���</param> /// <param name="eventHandler">���ֵ��ѡ�У�����ô�ί��</param> /// <param name="e">ί���¼�����</param> /// <returns>�ҵ���<see cref="ListItem"/>�����û�ҵ����ؿ�����</returns> public static ListItem SetListControlSafely(ListControl listCtrl, string value, string text, bool addIfNotExists, bool addEmptyItem, EventHandler eventHandler, EventArgs e) { if (listCtrl == null) { return null; } ListItem li = null; if (value != null) { li = listCtrl.Items.FindByValue(value); if (li == null && addIfNotExists) { if (string.IsNullOrEmpty(text)) { text = string.Format("({0})", value); } li = new ListItem(text, value); } } if (addEmptyItem) { listCtrl.Items.Insert(0, string.Empty); } if (li != null) { listCtrl.ClearSelection(); li.Selected = true; if (eventHandler != null) { eventHandler(listCtrl, e); } } return li; }