在项目中,我有一些用于表单元素的自定义 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;
}
出于某种原因,即使我单步执行代码并看到“控件”被分配给我的网络用户控件,“if”语句也总是返回 false。此代码位于项目中的另一个位置,除了在其他位置使用标准 .net 控件“TextBox”和“DropDownList”之外完全相同,并且在其他位置代码有效。
有谁知道为什么这不适用于网络用户控件?
更新:
嗯,在调试中我发现了这一点:
?control.GetType();
BaseType: {Name = "DropDownListField" FullName = "WebUI.UserControls.Fields.DropDownListField"}
?typeof(DropDownListField);
BaseType: {Name = "UserControl" FullName = "System.Web.UI.UserControl"}
所以 typeof 只是认识到它们是用户控件,而不是看起来的完整类型。
有谁知道我如何检查特定的用户控件类型?
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:
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.
Does anybody know why this wouldn't work with web user controls?
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.
Does anybody know how I would check for a specific user control type?
发布评论
评论(3)
我猜它们不是同一类型,请使用调试来找出实际类型。
另外,请尝试使用“is ' 关键字代替。
I'm guessing they aren't the same type, use debugging to find out the actual type.
Also, try using the 'is' keyword instead.
PS:你说 if (control is DropDownListField) 可能会更清晰,
我不记得视图是否直接在 Controls 中包含其子级,但如果 Controls 仅包含一个元素(这将是一个容器),我不会感到惊讶某种程度的。因此,您的控件可能位于 Controls[0].Controls 中,甚至更靠下。我建议您创建一种递归查找子项的方法。
实际上,您的控件都应该实现一个通用接口(例如
:)。您生成的代码会更加干净。
PS: It might be cleaner for you to say if (control is 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.
Actually, your controls should all implement a common interface (example:
). Your resulting code would be much cleaner.
c2.GetType().ToString() == "System.Web.UI.WebControls.Label"
c2.GetType().ToString() == "System.Web.UI.WebControls.Label"