Ejemplo n.º 1
0
        /// <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;
            }
        }
Ejemplo n.º 2
0
 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;
 }
Ejemplo n.º 3
0
		/// <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;
		}