A question submitted by J. Han introduced a NullRef exception issue. While coming up with a solution I found some bizarre behavior with Page_Load and LoginView.

There seems be a disconnect in accessibility of controls until the page is fully loaded. This is causing the server side controls to be Null referenced unless you forcibly expose them.


<%@ Page Language="C#" MasterPageFile="~/Site2.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Test.WebForm1" %>

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <div ID="success" style="visibility:visible" runat="server">         
        <asp:button id="btnClick" onclick="btnClick_Click" runat="server" text="Click Me!" />

    <div ID="fail" style="visibility:hidden" runat="server">
        <asp:Button ID="btnDontClick" onclick="btnDontClick_Click" runat="server" Text="Do Not Click Me..." />


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Test
    public partial class WebForm1 : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            if (Page.FindControl("fail") != null)
                fail.Style.Add("visibility", "hidden");
                success.Style.Add("visibility", "Visible");
                fail.Style.Add("visibility", "hidden");
                success.Style.Add("visibility", "Visible");

        protected void btnClick_Click(object sender, EventArgs e)
            fail.Style.Add("visibility", "Visible");
            success.Style.Add("visibility", "hidden");

        protected void btnDontClick_Click(object sender, EventArgs e)
            fail.Style.Add("visibility", "hidden");
            success.Style.Add("visibility", "Visible");


<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site2.master.cs" Inherits="Test.Site2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <asp:ContentPlaceHolder ID="head" runat="server">
    <form id="form1" runat="server">
    <div id="body">
        <asp:LoginView ID="LoginView1" runat="server" ViewStateMode="Disabled">
                    <asp:ContentPlaceHolder runat="server" ID="MainContent" />



The first time you run this you should get a NullRef exception on the fail.Style.Add("visibility","hidden"); line. We should note that if you comment out these two Style.Add lines, the page renders and the button works as designed.

Now to add the confusion...

Uncomment the commented FindControl line:


Now this time you run it, it should behave as one would expect.

The Question

Why does this line of code expose the controls? Is there a "control cache" that isn't getting refreshed upon entry of Page_Load? Running FindControl() refreshes this cache? Why aren't these controls exposed already?

Maybe this is by design, but unlikely.

Hopefully someone on here has some insight into why this happens....

Related posts

Recent Viewed