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.
The Problem
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>
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.
The Code
This is the code I'm using in the code behind.
fail.Style.Add("visibility", "hidden");
success.Style.Add("visibility", "Visible");
I have also tried:
fail.Attributes.Add("style", "visibility:Visible");
success.Attributes.Add("style", "visibility:Visible");
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?
The code for 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)
        {
        }
    }
}
The code for 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>
Part of 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>
 
    