Mam niestandardową kontrolę użytkownika DatePicker.cs. Wewnątrz innego fragmentu kodu mam zbiór kontrolek, w których sprawdzam typ kontrolki i wykonuję logikę na podstawie typu. Mój problem jest następujący:Jak uzyskać "typof" niestandardowej kontroli użytkownika
typeof(DatePicker)
Evalutes do:
{Name = "DatePicker" FullName = "cusitecore.cedarsc.UserControls.DatePicker"}
Ale kiedy uruchomić debugger i spojrzeć na typ kontroli, który jest na mojej sieci tworzą Jest:
{Name = "cedarsc_usercontrols_datepicker_ascx" FullName = "ASP.cedarsc_usercontrols_datepicker_ascx"}
Te dwie rzeczy nie są równe, więc poprawna logika nie jest oceniana. Próbowałem już przy użyciu Type.GetType ("ASP.cedarsc_usercontrols_datepicker_ascx"), ale zwraca wartość null.
EDIT
Oto, co usiłuję zrobić:
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");
}
}
}
Czy możesz podać trochę więcej kontekstu? Jak wygląda Twój kod, próbując dokonać porównania między tymi dwoma typami? –
Program ASP.NET tworzy własny typ dziedziczony po kontroli użytkownika.Użyj słowa kluczowego 'is' do porównań i spróbuj' control.GetType(). BaseType' dla ekstrakcji. –
@Jaroslav to jest odpowiedź. Uderz w dół, abyśmy mogli dać ci kredyt :) – Kyle