简体   繁体   English

C#ASP.NET GetType()与WebUserControl问题

[英]C# ASP.NET GetType() with WebUserControl issue

In the project I have some custom WebUserControls for form elements (they encapsulate some standard validators and other system specific functions). 在项目中,我有一些用于表单元素的自定义WebUserControls(它们封装了一些标准验证器和其他系统特定功能)。 My user controls are "DropDownListField" and "TextBoxField". 我的用户控件是“ DropDownListField”和“ 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. 由于某些原因,即使我单步执行代码并看到“控件”正在分配给我的Web用户控件,“ if”语句也始终返回false。 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. 该代码在项目中的另一个位置完全相同,除了在其他位置使用标准.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? 有人知道我将如何检查特定的用户控件类型吗?

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 '关键字代替。

PS: It might be cleaner for you to say if (control is DropDownListField) PS:您说(控件为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. 我不记得视图是否直接在Controls中包含其子级,但是如果Controls仅包含一个元素(可能是某种容器),我也不会感到惊讶。 Therefore, your controls are potentially in Controls[0].Controls or even further down. 因此,您的控件可能位于Controls [0] .Controls中,甚至更进一步。 I would advise you create a method that finds the child recursively. 我建议您创建一种递归查找子项的方法。

Actually, your controls should all implement a common interface (example: 实际上,您的控件都应实现一个通用接口(例如:

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

). )。 Your resulting code would be much cleaner. 您生成的代码将更加简洁。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM