gpt4 book ai didi

c# - 如何获取自定义用户控件的 "typeof"

转载 作者:可可西里 更新时间:2023-11-01 08:42:14 27 4
gpt4 key购买 nike

我有一个自定义用户控件 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");
}
}
}

最佳答案

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

要进行比较,请使用 is 运算符。
对于提取,请使用 control.GetType().BaseType

关于c# - 如何获取自定义用户控件的 "typeof",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4949078/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com