由于找不到控件而导致的ASP.net空引用异常
本文关键字:net ASP 引用 异常 找不到 控件 | 更新日期: 2023-09-27 18:10:54
我有这个例外,我不知道为什么它发生了。我搜索了这个网站和其他网站,试图找到一个解决方案,但到目前为止都没有奏效。
在我的.aspx页面中有以下两个div:
<div ID="success" style="visibility:hidden" runat="server">
// buttons and textfields
</div>
<div ID="fail" style="visibility:hidden" runat="server">
// buttons and textfields
</div>
在页面加载时,我希望一个根据某些标准成为可见的。但是当我试图在代码中瞄准它们以改变它们的可见性时,它会给我一个NullReferenceException,指向试图改变它们可见性的代码。
这是我在后面使用的代码。
fail.Style.Add("visibility", "hidden");
success.Style.Add("visibility", "Visible");
我也试过了:
fail.Attributes.Add("style", "visibility:Visible");
success.Attributes.Add("style", "visibility:Visible");
我已经在另一个。aspx页面上做了同样的操作,它没有给我这个NullReferenceException在该页上。所以我不知道发生了什么。有人能帮忙吗?
newBin.aspx.cs
代码using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Data.Common;
namespace SMTInventory
{
public partial class newBin : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.PreviousPage != null)
{
HiddenField pkid_box = (HiddenField)Page.PreviousPage.FindControl("locationID");
string pkid = pkid_box.Value;
locationID.Value = pkid;
success.Style.Add("visibility", "Visible");
}
else
{
if (Page.FindControl("fail") != null)
{
fail.Style.Add("visibility", "hidden");
success.Style.Add("visibility", "Visible");
}
else
{
fail.Style.Add("visibility", "hidden");
success.Style.Add("visibility", "Visible");
}
}
}
protected void btnNewLocation_Click(object sender, EventArgs e)
{
Server.Transfer("newLocation.aspx", true);
}
protected void btnNewBin_Click(object sender, EventArgs e)
{
}
}
}
newBin.aspx的代码<%@ Page Title="New Bin" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="newBin.aspx.cs" Inherits="SMTInventory.newBin" %>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<hgroup class="title">
<h1><%: Title %>.</h1>
<h2>Add a New Bin</h2>
</hgroup>
<article>
<div ID="success" style="visibility:hidden" runat="server">
<asp:HiddenField ID="locationID" value="" runat="server" />
How many racks are in this bin? <asp:TextBox ID="binCount" runat="server" /><br />
<asp:button id="btnNewBin" onclick="btnNewBin_Click" runat="server" text="Add Bin" />
</div>
<div ID="fail" style="visibility:hidden" runat="server">
<p>This page cannot be used without first creating a new location.<br /></p>
<asp:Button ID="btnNewLocation" onclick="btnNewLocation_Click" runat="server" Text="Create New Location" />
</div>
</article>
<aside>
<h3>Aside Title</h3>
<p>
Use this area to provide additional information.
</p>
<ul>
<li><a runat="server" href="~/">Home</a></li>
<li><a runat="server" href="~/About">About</a></li>
<li><a runat="server" href="~/Contact">Contact</a></li>
</ul>
</aside>
</asp:Content>
网站的一部分。<div id="body">
<asp:LoginView runat="server" ViewStateMode="Disabled">
<AnonymousTemplate>
<asp:ContentPlaceHolder runat="server" ID="FeaturedContent" />
<section class="content-wrapper main-content clear-fix">
<asp:ContentPlaceHolder runat="server" ID="MainContent" />
</section>
<asp:ContentPlaceHolder runat="server" ID="NewContent" />
</AnonymousTemplate>
<LoggedInTemplate>
</LoggedInTemplate>
</asp:LoginView>
</div>
<div id="body">
<asp:LoginView runat="server" ViewStateMode="Disabled">
<AnonymousTemplate>
<asp:ContentPlaceHolder runat="server" ID="FeaturedContent" />
<section class="content-wrapper main-content clear-fix">
<asp:ContentPlaceHolder runat="server" ID="MainContent" />
</section>
<asp:ContentPlaceHolder runat="server" ID="NewContent" />
</AnonymousTemplate>
<LoggedInTemplate>
</LoggedInTemplate>
</asp:LoginView>
</div>
根据我的经验,div不像ASP控件那样注册到服务器,所以直接调用它们会产生不好的结果。当对控件进行更改时,例如添加样式,请确保您告诉ASP您有哪种控件。
例如:HtmlGenericControl _fail = (HtmlGenericControl)Page.FindControl("fail");
_fail.Style.Item("visibility") = "hidden";
编辑:问题在于ContentPlaceHolder嵌套在LoginView。深入到控件应该会暴露它们。
的例子:
LoginView temp = (LoginView)this.Master.FindControl("LoginView1");
ContentPlaceHolder tempp = (ContentPlaceHolder)temp.FindControl("MainContent");
HtmlGenericControl _fail = (HtmlGenericControl)tempp.FindControl("fail");
如果您创建了一些类变量来指向这些控件,并在页面加载时分配它们,那么您可以在代码中的任何地方调用它们。
为了使解决方案更加混乱,如果您只添加:
LoginView temp = (LoginView)this.Master.FindControl("LoginView1");
ContentPlaceHolder tempp = (ContentPlaceHolder)temp.FindControl("MainContent");
到Page_Load,它公开了控件,所以你可以调用fail.Style。直接添加("visibility","hidden")。ASP列举控件的时间似乎有些延迟。在LoginView上调用FindControls(),似乎刷新了控件"缓存",暴露了您期望的控件