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

发布于 2024-10-16 19:21:58 字数 1003 浏览 2 评论 0 原文

在项目中,我有一些用于表单元素的自定义 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

万劫不复 2024-10-23 19:21:58

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

另外,请尝试使用“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.

别念他 2024-10-23 19:21:58

PS:你说 if (control is DropDownListField) 可能会更清晰,

我不记得视图是否直接在 Controls 中包含其子级,但如果 Controls 仅包含一个元素(这将是一个容器),我不会感到惊讶某种程度的。因此,您的控件可能位于 Controls[0].Controls 中,甚至更靠下。我建议您创建一种递归查找子项的方法。

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

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

:)。您生成的代码会更加干净。

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:

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

). Your resulting code would be much cleaner.

独享拥抱 2024-10-23 19:21:58

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文