Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the "typeof" of a custom user control

I have a custom user control DatePicker.cs. Inside of another piece of code I have a collection of controls where I am checking the type of the control and doing some logic based on the type. My problem is the following:

typeof(DatePicker)

Evalutes to:

{Name = "DatePicker" FullName = "cusitecore.cedarsc.UserControls.DatePicker"}

But when I run the debugger and look at the type of the control that is on my web form it is:

{Name = "cedarsc_usercontrols_datepicker_ascx" FullName = "ASP.cedarsc_usercontrols_datepicker_ascx"}

These two things aren't equal so the correct logic isn't getting evaluated. I've tried using Type.GetType("ASP.cedarsc_usercontrols_datepicker_ascx") but this returns null.

EDIT

Here's what I'm trying to do:

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");
        }
    }
}
like image 689
Kyle Avatar asked Feb 09 '11 18:02

Kyle


People also ask

What is custom control user control?

CustomControl is a loosely coupled control w.r.t code and UI while UserControl is a tightly coupled control w.r.t code and UI. When using CustomControl UI can be changed in different projects but for a UserControl UI is fixed and can't have different looks in different project.

What is the difference between user control and custom control *?

UserControl : A control which can reuse the Components in the Applications. The control can be defined in both Xaml and Code-Behind. CustomControl : An UserInterface element that have a distinct behavior which is said as CustomControl.


1 Answers

ASP.NET creates it's own type inherited from user controls.

For comparisons use the is operator.
For extractions use control.GetType().BaseType.

like image 110
Jaroslav Jandek Avatar answered Oct 15 '22 02:10

Jaroslav Jandek