【发布时间】:2015-11-26 23:47:24
【问题描述】:
我遇到了这个异常,我不知道为什么会这样。我搜索了这个网站和其他人试图找到解决方案,但到目前为止都没有成功。
问题
我的 .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>
Site.Master 的一部分
<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>
【问题讨论】:
-
尝试将
ClientIDMode="Static"添加到两个 div 之一。我非常怀疑它会有所作为,但值得一试。 -
我喜欢 Daniel A White 在他未阅读时将其标记为已询问的方式,因为我已经在该站点上搜索了 NullReferenceException 和解决方案,但 NONE 已成功。 Nicholas V. 我会试试看它是否会改变。
-
我确实跟踪它并尝试调试它。它告诉我我的引用未设置为对象的实例。我已经在我的另一个页面上完成了这个确切的代码,它没有给我这个异常。
-
您的页面指令是否为当前页面正确格式化?这将是您的 ASPX 代码的第一行。
-
用户是匿名的还是 LoggedInTemplate 为简洁起见为空?
标签: c# asp.net nullreferenceexception web-controls