我有这个例外,我无法弄清楚它为什么会发生。我搜索了这个网站和其他人试图找到一个解决方案,但到目前为止还没有工作。
我的.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。所以我不知道发生了什么。有人可以帮忙吗?
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)
{
}
}
}
<%@ 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>
答案 0 :(得分:1)
根据我的经验,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.Add(&#34; visibility&#34;,&#34; hidden&#34;)。关于控件何时被ASP枚举似乎有些延迟。在LoginView上调用FindControls()似乎刷新了控件&#34; cache&#34;像你期望的那样暴露控件。