什么会导致 FindControl() 抛出 NullReferenceException?

发布于 2024-10-14 18:48:29 字数 1810 浏览 10 评论 0原文

我正在构建一个用户控件(ASP.NET 3.5,使用 C#)。

该控件很大程度上基于另一个类似的控件(不幸的是,剪切和粘贴继承,但没有简单的方法来抽象它)。给我带来麻烦的行是直接从其他控件复制的,大部分显示也是如此。

相关代码行如下:

Panel pnlForm = (Panel)e.Item.FindControl("pnlForm");
Literal ltrAvailableCount = (Literal)e.Item.FindControl("ltrAvailableCount");

DropDownList drpLanguage = (DropDownList)pnlForm.FindControl("drpLanguage");
DropDownList drpShipTo = (DropDownList)pnlForm.FindControl("drpShipTo");
HiddenField hdnAvailableProductId = (HiddenField)pnlForm.FindControl("hdnAvailableProductId");

DropDownList drpQuantity = (DropDownList)pnlForm.FindControl("drpQuantity");
HiddenField hdnSelectedStyle = (HiddenField)e.Item.FindControl("hdnSelectedStyle");
Label lblStyleName = (Label)e.Item.FindControl("lblSelectedStyle");

上面的pnlForm被正确找到,ltrAvailableCount也是如此。

如果我跳到drpQuantity,它和以下几行工作正常。但是,当我运行 drpLanguagedrpShipTohdnAvailableProductId 的行时,FindControl 会抛出 NullReferenceException< /代码>。

我的意思并不是说它返回“null”,并且我尝试访问返回对象的属性,FindControl 方法抛出异常。根据 MSDN 库,这是不可能的 - FindControl 不会列出任何可能引发错误的内容,它只是说如果找不到 Control,则返回 null。

什么会导致 FindControl 出现 NullReferenceException?

-----------------编辑----------------------------

我还应该提到,如果我立即运行三个有问题的行中的任何一个窗口,我得到了正确的结果。当我在调试时检查它时,我还可以看到 pnlFormControlCollection 中的控件。

-----------------第二次编辑-----------------

为了确认,我添加了另一行: DropDownList notThere = (DropDownList)pnlForm.FindControl("notHere"); 控件 notHere 不在页面上的任何位置。上面的行编译(当然),当我运行调试器时,它运行良好。演员阵容进展顺利。变量 notThere 就是 null。

此问题的根本原因不是 FindControl 未能找到控件并尝试将 null 转换为 DropDownList 或其他控件

I have a user control I'm building (ASP.NET 3.5, using C#).

This control is largely based on another, similar control (cut & paste inheritance, unfortunately, but there's no easy way to abstract this). The lines that are giving me trouble are directly copied from the other control, as is the majority of the display.

The relevant lines of code are as follows:

Panel pnlForm = (Panel)e.Item.FindControl("pnlForm");
Literal ltrAvailableCount = (Literal)e.Item.FindControl("ltrAvailableCount");

DropDownList drpLanguage = (DropDownList)pnlForm.FindControl("drpLanguage");
DropDownList drpShipTo = (DropDownList)pnlForm.FindControl("drpShipTo");
HiddenField hdnAvailableProductId = (HiddenField)pnlForm.FindControl("hdnAvailableProductId");

DropDownList drpQuantity = (DropDownList)pnlForm.FindControl("drpQuantity");
HiddenField hdnSelectedStyle = (HiddenField)e.Item.FindControl("hdnSelectedStyle");
Label lblStyleName = (Label)e.Item.FindControl("lblSelectedStyle");

pnlForm in the above is found correctly, as is ltrAvailableCount.

If I skip down to drpQuantity, it and the following lines work fine. However, when I run the lines for drpLanguage, drpShipTo, or hdnAvailableProductId FindControl throws a NullReferenceException.

I do not mean it returns 'null' and I try to access a property of the returned object, the method FindControl throws the exception. According to the MSDN library, this isn't possible - FindControl doesn't list anything as a potential thrown error, it just says that if it can't find the Control, it returns null.

What can cause FindControl to NullReferenceException?

-----------------Edit---------------

I should also mention that if I run any of the three problematic lines in the immediate window, I get the correct results. I can also see the controls in the ControlCollection of pnlForm when I inspect it while debugging.

-----------------2nd Edit-------------------

Just to confirm, I added another line:
DropDownList notThere = (DropDownList)pnlForm.FindControl("notHere");
The control notHere is not anywhere on the page. The above line compiles (of course) and when I run the debugger, it runs fine. The cast goes fine. The variable notThere simply is null.

The underlying cause of this issue is NOT FindControl failing to find the controls and trying to cast null to a DropDownList or other Control

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

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

发布评论

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

评论(3

梦在深巷 2024-10-21 18:48:29

导致问题的不是 FindControl。

您的编码方式是在一行上执行两个操作 - FindControl(),然后转换为控件数据类型。这是轰炸式的强制转换,因为 FindControl 调用的结果是 null 值,而您正尝试将 null 强制转换为控件。

这个问题咬了我很多次,所以我学会了将其编码为

object oDropDown1 = pnlForm.FindControl("DropDown1");

// then check if oDrowpDown 1 is null and cast if it's safe.

if(oDropDown1 != null)
{
   // here it's safe to cast.
}

It is NOT FindControl that's causing the problem.

The way you've got it coded it's performing two operations on one line - FindControl() and then the cast to the control datatype. It's the cast that's bombing, because the results of the FindControl call is a null value, and you're attempting to cast null to a control.

This one bit me many times, so I learned to code it as

object oDropDown1 = pnlForm.FindControl("DropDown1");

// then check if oDrowpDown 1 is null and cast if it's safe.

if(oDropDown1 != null)
{
   // here it's safe to cast.
}
深海少女心 2024-10-21 18:48:29

null 转换为 DropDownList 将导致异常。

如果 pnlForm.FindControl("drpQuantity") 返回 null,则无法强制转换它;)

但是这将起作用:

System.Web.UI.WebControls.DropDownList drpQuantity = 
   pnlForm.FindControl("drpQuantity") as System.Web.UI.WebControlsDropDownList;

if(drpQuantity!=null){
   //use drpQuantity here
}

as 运算符用于执行兼容类型之间的转换,as运算符类似于强制转换,只不过它在转换失败时生成 null,而不是引发异常。

无论如何findcontrol的使用仅适用于数据生成的控件,在所有其他情况下都使用接口 (-实现)与用户控件上的控件进行通信。

恕我直言,findcontrol 是一个在全球范围内被滥用的功能......(小猫因使用它而被杀死)

casting null to a DropDownList will result in an exception.

if pnlForm.FindControl("drpQuantity") returns null you cannot cast it ;)

however this will work:

System.Web.UI.WebControls.DropDownList drpQuantity = 
   pnlForm.FindControl("drpQuantity") as System.Web.UI.WebControlsDropDownList;

if(drpQuantity!=null){
   //use drpQuantity here
}

The as operator is used to perform conversions between compatible type, The as operator is like a cast except that it yields null on conversion failure instead of raising an exception.

anyway, the use of findcontrol is only intended for data-generated controls, in all other cases use an interface (-implementation) to communicate with controls on usercontrols.

IMHO findcontrol is a feature that is abused globally.... ( kittens are killed for using it )

暮年 2024-10-21 18:48:29

你得到一个 NullReferenceException,它没有找到(你正在寻找的)控件,

尝试:

DropDownList drpLanguage = (DropDownList)(pnlForm.FindControl("drpLanguage"));
DropDownList drpShipTo = (DropDownList)(pnlForm.FindControl("drpShipTo"));
HiddenField hdnAvailableProductId = (HiddenField)(pnlForm.FindControl("hdnAvailableProductId"));

You get a NullReferenceException, of it does not find a (the) control (you were looking for)

try:

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