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.
WebForm1.aspx
<%@ 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>
<div ID="fail" style="visibility:hidden" runat="server">
<asp:Button ID="btnDontClick" onclick="btnDontClick_Click" runat="server" Text="Do Not Click Me..." />
</div>
</asp:Content>
WebForm1.aspx.cs
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)
{
//this.Master.FindControl("LoginView1").FindControl("MainContent");
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 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");
}
}
}
Site2.Master
<%@ 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">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div id="body">
<asp:LoginView ID="LoginView1" runat="server" ViewStateMode="Disabled">
<AnonymousTemplate>
<asp:ContentPlaceHolder runat="server" ID="MainContent" />
</AnonymousTemplate>
<LoggedInTemplate>
</LoggedInTemplate>
</asp:LoginView>
</div>
</form>
</body>
</html>
Testing
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:
this.Master.FindControl("LoginView1").FindControl("MainContent");;
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....