c# - 空引用异常 - 为什么?

标签 c# asp.net

Panel pnl_Confirmation = (Panel)form1.FindControl("ConfirmationPanel") as Panel;
    pnl_Confirmation.Visible = false;

我在上面的代码中一直收到“NullReferenceException”。 “ConfirmationPanel”是我根据表单是否成功提交显示/隐藏的面板。

面板包裹在 LoginView 控件中,这就是我不能直接调用“ConfirmationPanel”的原因:

<asp:LoginView ID="LoginView1" runat="server">
<LoggedInTemplate> 
<asp:Panel id="ConfirmationPanel" runat="server" Wrap="False">

...

为什么这个是空的?它应该引用 ID 为“ConfirmationPanel”的面板,不是吗?

谢谢

最佳答案

试试这个:

Panel pnl_Confirmation = LoginView1.FindControl("ConfirmationPanel") as Panel;
if(pnl_Confirmation != null)
   pnl_Confirmation.Visible = false;

关于c# - 空引用异常 - 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4229442/

相关文章:

asp.net - 使用用户控件时,元素<element>在Visual Studio中不是已知的元素警告

c# - 如何使 beforeunload 事件仅在浏览器 session 退出时触发

c# - System.Net.Http 的 .NET Framework 版本高于项目

asp.net - 如何在aspx(javascript)中找到控件在模板中的ClientId

c# - aPress 的 pro asp.net mvc 3 书中的一些错误

c# - 空传播运算符和 Newtonsoft.Json 10.0.3 异常

c# - 文件夹的简单 C# USING 语句

javascript - 在 Angular 中将对象数组传递给 POST

c# - System.Web.HttpUtility.HtmlDecode 给出不同的结果

c# - 在 C# 中将数据传递给 child 的 STDIN