带有WebUserControl问题的C#ASP.NET GetType()

时间:2022-07-17 03:44:30

In the project I have some custom WebUserControls for form elements (they encapsulate some standard validators and other system specific functions). My user controls are "DropDownListField" and "TextBoxField". In the code behind of a page I have this code:

在项目中,我有一些表单元素的自定义WebUserControls(它们封装了一些标准的验证器和其他系统特定的功能)。我的用户控件是“DropDownListField”和“TextBoxField”。在页面后面的代码我有这个代码:

string parameterValue = null;
foreach (object control in myMultiView.Views[myMultiView.ActiveViewIndex].Controls)
{
    if (control.GetType() == typeof(DropDownListField))
        parameterValue = ((DropDownListField)control).Value;
    if (control.GetType() == typeof(TextBoxField))
        parameterValue = ((TextBoxField)control).Value;
}

For some reason the "if" statements always return false even when I step through the code and see that "control" is getting assigned my web user control. This code is in another place in the project exactly the same except in the other location the standard .net controls "TextBox" and "DropDownList" are used and in the other location the code works.

由于某种原因,“if”语句总是返回false,即使我单步执行代码并看到“控件”被分配给我的Web用户控件。此代码在项目的另一个位置完全相同,除了在其他位置使用标准的.net控件“TextBox”和“DropDownList”,并在代码的其他位置工作。

Does anybody know why this wouldn't work with web user controls?

有谁知道为什么这不适用于Web用户控件?

UPDATE: Hmm so in debugging I found this:

更新:嗯所以在调试中我发现了这个:

?control.GetType();
BaseType: {Name = "DropDownListField" FullName = "WebUI.UserControls.Fields.DropDownListField"}
?typeof(DropDownListField);
BaseType: {Name = "UserControl" FullName = "System.Web.UI.UserControl"}

So typeof is just recognizing they are user controls not the full type it seems.

因此,typeof只是认识到它们是用户控件而不是它看起来的完整类型。

Does anybody know how I would check for a specific user control type?

有人知道如何检查特定的用户控件类型吗?

3 个解决方案

#1


3  

I'm guessing they aren't the same type, use debugging to find out the actual type.

我猜他们不是同一类型,使用调试来找出实际类型。

Also, try using the 'is' keyword instead.

另外,请尝试使用'is'关键字。

#2


2  

PS: It might be cleaner for you to say if (control is DropDownListField)

PS:你说if(控件是DropDownListField)可能更干净

I don't recall if a view directly includes its children in Controls, but I wouldn't be surprised if Controls contained only one element, which would be a container of some sorts. Therefore, your controls are potentially in Controls[0].Controls or even further down. I would advise you create a method that finds the child recursively.

我不记得一个视图是否直接在Controls中包含它的子节点,但是如果Controls只包含一个元素,那么我也不会感到惊讶。因此,您的控件可能位于Controls [0] .Controls中,甚至更远。我建议你创建一个递归查找子的方法。

Actually, your controls should all implement a common interface (example:

实际上,你的控件都应该实现一个通用的接口(例如:

interface ICustomFieldWithValue { string Value {get; set; }}

). Your resulting code would be much cleaner.

)。您生成的代码会更清晰。

#3


1  

c2.GetType().ToString() == "System.Web.UI.WebControls.Label"

c2.GetType()。ToString()==“System.Web.UI.WebControls.Label”

#1


3  

I'm guessing they aren't the same type, use debugging to find out the actual type.

我猜他们不是同一类型,使用调试来找出实际类型。

Also, try using the 'is' keyword instead.

另外,请尝试使用'is'关键字。

#2


2  

PS: It might be cleaner for you to say if (control is DropDownListField)

PS:你说if(控件是DropDownListField)可能更干净

I don't recall if a view directly includes its children in Controls, but I wouldn't be surprised if Controls contained only one element, which would be a container of some sorts. Therefore, your controls are potentially in Controls[0].Controls or even further down. I would advise you create a method that finds the child recursively.

我不记得一个视图是否直接在Controls中包含它的子节点,但是如果Controls只包含一个元素,那么我也不会感到惊讶。因此,您的控件可能位于Controls [0] .Controls中,甚至更远。我建议你创建一个递归查找子的方法。

Actually, your controls should all implement a common interface (example:

实际上,你的控件都应该实现一个通用的接口(例如:

interface ICustomFieldWithValue { string Value {get; set; }}

). Your resulting code would be much cleaner.

)。您生成的代码会更清晰。

#3


1  

c2.GetType().ToString() == "System.Web.UI.WebControls.Label"

c2.GetType()。ToString()==“System.Web.UI.WebControls.Label”