本文整理汇总了C#中System.Web.UI.WebControls.ListControl.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# ListControl.GetType方法的具体用法?C# ListControl.GetType怎么用?C# ListControl.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Web.UI.WebControls.ListControl
的用法示例。
在下文中一共展示了ListControl.GetType方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetFormItemListControl
/// <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;
}
}
示例2: SelectedByValue
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;
}
示例3: TrySetValue
/// <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;
}