如何获得“类型”自定义用户控件 [英] How to get the "typeof" of a custom user control

查看:64
本文介绍了如何获得“类型”自定义用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义用户控件DatePicker.cs。在另一段代码中,我有一组控件,在其中检查控件的类型并根据该类型做一些逻辑。我的问题如下:

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)

评估为:

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

但是当我运行调试器并查看调试器的类型时在我的Web表单上的控件是:

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"}

这两个条件不相等,因此正确的逻辑未得到评估。我尝试使用Type.GetType( ASP.cedarsc_usercontrols_datepicker_ascx),但是返回null。

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

这就是我要做的事情:

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");
        }
    }
}


推荐答案

ASP.NET创建它自己的类型,该类型是从用户控件继承的。

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

要进行比较,请使用 is 运算符。

对于提取,请使用 control.GetType()。BaseType

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

这篇关于如何获得“类型”自定义用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆