我有一个自定义用户控件DatePicker.cs。在另一段代码中,我有一组控件,我正在检查控件的类型并根据类型做一些逻辑。我的问题如下:
typeof(DatePicker)
评估:
{Name = "DatePicker" FullName = "cusitecore.cedarsc.UserControls.DatePicker"}
但是当我运行调试器并查看我的Web表单上的控件类型时,它是:
{Name = "cedarsc_usercontrols_datepicker_ascx" FullName = "ASP.cedarsc_usercontrols_datepicker_ascx"}
这两件事情并不相同,所以没有评估正确的逻辑。我尝试过使用Type.GetType(“ASP.cedarsc_usercontrols_datepicker_ascx”),但这会返回null。
修改
这是我正在尝试做的事情:
private readonly Dictionary<Type, ControlType?> _controlTypes = new Dictionary<Type, ControlType?>
{
{typeof(CheckBox), ControlType.CheckBox},
{typeof(CheckBoxList), ControlType.CheckBoxList},
{typeof(DropDownList), ControlType.DropDownList},
{typeof(HiddenField), ControlType.HiddenField},
{typeof(ListBox), ControlType.ListBox},
{typeof(RadioButton), ControlType.RadioButton},
{typeof(RadioButtonList), ControlType.RadioButtonList},
{typeof(TextBox), ControlType.TextBox},
{typeof(Label), ControlType.Label},
{typeof(DatePicker), ControlType.DatePicker},
{typeof(CustomSelect), ControlType.CustomSelect}
};
private void PopulateFields(Control control)
{
ControlType? controlType;
_controlTypes.TryGetValue(control.GetType(), out controlType);
// recurse over the children
if (control.Controls.Count > 0 && controlType == null) // don't want to recurse into children of controls we are reading values of
{
foreach(Control childControl in control.Controls)
{
PopulateFields(childControl);
}
}
if (controlType != null)
{
switch (controlType)
{
case ControlType.CheckBox:
case ControlType.RadioButton:
CheckBox checkBox = control as CheckBox;
if (checkBox != null)
_fields.AddFieldValue(checkBox.ID, checkBox.Checked ? "Checked" : "Not Checked");
break;
case ControlType.CheckBoxList:
case ControlType.ListBox:
case ControlType.RadioButtonList:
ListControl listControl = control as ListControl;
if (listControl != null)
_fields.AddFieldValue(listControl.ID, String.Join(", ", listControl.Items.Cast<ListItem>().Where(item => item.Selected).Select(item => item.Value).ToArray()));
break;
case ControlType.DropDownList:
DropDownList dropDownList = control as DropDownList;
if (dropDownList != null)
_fields.AddFieldValue(dropDownList.ID, dropDownList.SelectedValue);
break;
case ControlType.HiddenField:
HiddenField hiddenField = control as HiddenField;
if (hiddenField != null)
_fields.AddFieldValue(hiddenField.ID, hiddenField.Value);
break;
case ControlType.TextBox:
TextBox textBox = control as TextBox;
if (textBox != null)
_fields.AddFieldValue(textBox.ID, textBox.Text);
break;
case ControlType.DatePicker:
DatePicker datePicker = control as DatePicker;
if (datePicker != null)
_fields.AddFieldValue(datePicker.ID, datePicker.Text);
break;
case ControlType.CustomSelect:
CustomSelect customSelect = control as CustomSelect;
if(customSelect != null)
_fields.AddFieldValue(customSelect.ID, customSelect.SelectedValue);
break;
case ControlType.Label:
Label label = control as Label;
if(label != null)
_fields.AddFieldLabel(label.AssociatedControlID, label.Text);
break;
default:
throw new Exception("Unhandled Control");
}
}
}
答案 0 :(得分:11)
ASP.NET创建自己继承自用户控件的类型。
要进行比较,请使用is
运算符
对于提取,请使用control.GetType().BaseType
。
答案 1 :(得分:2)
您的帖子并未详细说明您打算如何使用此帖子,但我的事件中使用的typeof()从未遇到过任何问题。例如,我将在处理悬停的事件中使用以下if语句:
if (sender.GetType() == typeof(Transparent_Panel))
其中Transparent_Panel是自定义用户控件。我从未与Tansparent_Panel进行任何特殊的幕后工作来完成这项工作。
答案 2 :(得分:1)
您可以尝试使用关键字is
。这不是完全相同的事情,但如果你要做的就是确定一个对象是否属于某种类型(或者扩展/实现一个类/接口)那么这应该可以解决问题。
当然,根据您的代码,这可能无济于事。
答案 3 :(得分:1)
ASP.NET 2.0及更高版本将按需将UserControl
编译到Temporary ASP.NET Files目录,因此当您在调试器中查看控件的类型时,您正在检查的类型是自动的由ASP.NET编译引擎生成。好消息是此类型继承自DatePicker
类型,因此以下代码应该用于测试给定的UserControl
是否实际上是DatePicker
:
typeof(DatePicker).IsAssignableFrom(userControl.GetType().BaseType)
或者,您始终可以在运行时创建DatePicker
UserControl
的实例,并通过以下方式检查类型等效:
LoadControl(typeof(DatePicker)).GetType() == userControl.GetType()