由于找不到控件而导致ASP.net Null参考异常 [英] ASP.net Null Reference Exception Due to Not Finding Controls

查看:84
本文介绍了由于找不到控件而导致ASP.net Null参考异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个例外,我无法弄清楚它为什么会发生.我搜索了该站点以及其他试图找到解决方案的站点,但到目前为止都没有奏效.

I am having this exception that I can't figure out why it is happening. I searched this site and others trying to find a solution but none have worked so far.

我的.aspx页中有以下两个div.

I have the following two div in my .aspx page.

<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,该异常指向试图更改其可见性的代码.

On page load, I want one to become Visible depending on some criterion. But when I try to target them in code behind to change their visibility, it gives me a NullReferenceException pointing to the code trying to change their visibility.

这是我在后面的代码中使用的代码.

This is the code I'm using in the code behind.

fail.Style.Add("visibility", "hidden");
success.Style.Add("visibility", "Visible");

我也尝试过:

fail.Attributes.Add("style", "visibility:Visible");
success.Attributes.Add("style", "visibility:Visible");

我已经在另一个.aspx页面上执行了完全相同的操作,但没有在该页面上给我这个NullReferenceException.所以我不知道发生了什么.有人可以帮忙吗?

I have done this exact same action on another .aspx page and it didn't give me this NullReferenceException on that page. So I have no idea what's going on. Can someone help please?

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>

推荐答案

以我的经验,DIV不会像ASP控件那样注册到服务器,因此直接调用它们会产生不好的结果.在更改控件(即添加样式)时,请确保您告诉ASP您拥有哪种控件.

In my experience, DIV's are not registered to the server like ASP controls are so calling them directly would produce bad results. When making changes to the controls, i.e. adding styles, make sure you tell ASP what kind of control you have.

例如:

HtmlGenericControl _fail = (HtmlGenericControl)Page.FindControl("fail");

_fail.Style.Item("visibility") = "hidden";

问题在于ContentPlaceHolder嵌套在LoginView中.深入研究控件应将其暴露.

The problem lies with the ContentPlaceHolder being nested in the LoginView. Drilling down to the controls should expose them.

示例:

LoginView temp = (LoginView)this.Master.FindControl("LoginView1");
ContentPlaceHolder tempp = (ContentPlaceHolder)temp.FindControl("MainContent");
HtmlGenericControl _fail = (HtmlGenericControl)tempp.FindControl("fail");

如果创建一些类变量以指向这些控件并在页面加载时分配它们,则可以在代码中的任意位置调用它们.

If you create some class variables to point to these controls and assign them on page load, you can then call them from wherever you want in your code.

如果仅添加以下内容,则将进一步混淆该解决方案:

To add further confusion to the solution, if you only add:

LoginView temp = (LoginView)this.Master.FindControl("LoginView1");
ContentPlaceHolder tempp = (ContentPlaceHolder)temp.FindControl("MainContent");

对Page_Load没什么影响,它公开了控件,因此您可以直接调用fail.Style.Add("visibility","hidden"). ASP枚举控件的时间似乎有些延迟.在LoginView上调用FindControls()似乎刷新了控件的缓存",使控件如您期望的那样公开.

to Page_Load and nothing else, it exposes the controls so you can call fail.Style.Add("visibility", "hidden") directly. There seems to be some delay as to when the controls are enumerated by ASP. Calling FindControls() on LoginView, appears to refresh the control "cache" exposing the controls as you would expect them to be.

这篇关于由于找不到控件而导致ASP.net Null参考异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆