Tôi có một điều khiển người dùng tùy chỉnh DatePicker.cs. Bên trong một đoạn mã khác, tôi có một bộ sưu tập các điều khiển nơi tôi đang kiểm tra loại điều khiển và thực hiện một số logic dựa trên loại. Vấn đề của tôi là như sau:Cách lấy "typeof" của điều khiển người dùng tùy chỉnh
typeof(DatePicker)
Evalutes tới:
{Name = "DatePicker" FullName = "cusitecore.cedarsc.UserControls.DatePicker"}
Nhưng khi tôi chạy debugger và xem xét các loại điều khiển đó là trên web của tôi hình thành đó là:
{Name = "cedarsc_usercontrols_datepicker_ascx" FullName = "ASP.cedarsc_usercontrols_datepicker_ascx"}
Hai điều này không bằng nhau nên logic chính xác không được đánh giá. Tôi đã thử bằng cách sử dụng Type.GetType ("ASP.cedarsc_usercontrols_datepicker_ascx") nhưng điều này trả về null.
EDIT
Dưới đây là những gì tôi đang cố gắng để làm:
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");
}
}
}
Bạn có thể cung cấp thêm một số ngữ cảnh không? Mã của bạn trông như thế nào đang cố gắng thực hiện so sánh giữa hai loại? –
ASP.NET tạo loại riêng của nó được kế thừa từ điều khiển người dùng.Sử dụng từ khóa 'is' để so sánh và thử' control.GetType(). BaseType' để trích xuất. –
@Jaroslav đây là câu trả lời. Slap mà xuống ở phía dưới để chúng tôi có thể cung cấp cho bạn tín dụng :) – Kyle