1

I've found threads solving this issue within Default.aspx and Default.aspx.cs, but applying those solutions to my problem don't fix my issue. None of them are helpful in this context:

I'm trying to access an element on my Site.master page within Default.aspx.cs. I want to hide this element if a certain condition isn't met. However, I cannot reference any elements within my Site.master page (either within Default.aspx.cs OR Site.master.cs)

Code:

Site.master:

<li><a runat="server" id="editLink" href="~/Edit">Edit</a></li>

Default.aspx.cs:

MasterPage master = this.Master;
Control linkControl = master.FindControl("editLink");
linkControl.Visible = false;

I've also tried:

Site.master:

<asp:Panel ID="Panel1" runat="server">
    <li><a runat="server" id="editLink" href="~/Edit">Edit</a></li>
</asp:Panel>

Default.aspx.cs:

((Panel)Master.FindControl("Panel1")).Visible = false;
//or..
//Panel testPanel = ((Panel)Master.FindControl("Panel1"));
//Panel1.Visible = false;

I don't know where to start. Thanks!

EDIT:

I've tried to apply resolutions from:

ASP.NET - Accessing Master Page elements form the Content Page

What is a NullReferenceException, and how do I fix it?

Specifically from the first:

adding the following to Default.aspx

<%@ MasterType virtualpath="~/Site.master" %>

adding the following to Default.aspx.cs

Panel testPanel = (Panel)Master.FindControl("Panel1");       
testPanel.Visible = true;

The above code is the suggested fix for the first thread, but I get the same error. The second thread seemed to suggest I was referencing the element before it was created thus returning the nullreferenceexception error, but I do not believe this is the case. Ultimately, I do not understand enough to utilize those threads.

EDIT

Site.master MCVE:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="SiteMaster" %>

<form id="wrapForm" runat="server">
    <asp:ScriptManager runat="server" EnablePageMethods="true"></asp:ScriptManager>
    <asp:ContentPlaceHolder ID="MainContent" runat="server">
      <asp:Panel ID="Panel1" runat="server">Test Text</asp:Panel>
    </asp:ContentPlaceHolder>
</form>

Default.aspx.cs MCVE:

protected void Page_Load(object sender, EventArgs e)
{
    Panel testPanel = (Panel)Master.FindControl("Panel1"); 
    testPanel.Visible = true;
}
Community
  • 1
  • 1
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it). Other possible duplicate: [ASP.NET - Accessing Master Page elements form the Content Page](http://stackoverflow.com/q/461911/1260204) – Igor Feb 06 '17 at 15:57
  • You haven't shown the context of where the control is within a master page. Is it in another control? It helps to make an [MCVE](http://stackoverflow.com/help/mcve). – mason Feb 06 '17 at 16:15
  • Maybe your development webserver is still loading the old assemblies? Could be an idea to shut down the server entirely and rebuild/run. – NicoE Feb 06 '17 at 16:39
  • @mason I attempted to add an MCVE - stripped everything from Site.master and included just the Page_Load method from Default.aspx.cs. Please let me know if I've done this incorrectly. Thanks! – Skettenring Feb 06 '17 at 17:02
  • @Igor Edited, as I do not believe either of those threads help - I'd attempted to utilize the solutions in those specific threads and they do not seem to help – Skettenring Feb 06 '17 at 17:05
  • which is null at runtime? `Master`, or `testPanel`? could you include the markup of Default.aspx as well? – Cee McSharpface Feb 06 '17 at 17:13
  • `MasterType` <= shouldn't that be `Reference` as per [ASP.NET - Accessing Master Page elements form the Content Page](http://stackoverflow.com/questions/461911/asp-net-accessing-master-page-elements-form-the-content-page) ? So `` – Igor Feb 06 '17 at 17:15
  • Your MCVE is helpful (although still not complete). Why do you have the panel inside of a `ContentPlaceHolder`? Does your page have an `asp:Content` that corresponds to `MainContent`? – mason Feb 06 '17 at 17:17
  • @Igor That was it. Thank you very much! (can I mark comments as solutions?) – Skettenring Feb 06 '17 at 17:20
  • @mason the ContentPlaceHolder is referenced within Default.aspx and all other pages, so I don't have to add a header every time. I must have organized this in an odd way - changing my reference to the Master page made it so I can reference the panel, so now I'll start researching best practices for organization. Thank you! – Skettenring Feb 06 '17 at 17:22
  • Content in a content place holder will not be included in the resulting page if the content page has a Content control corresponding to that content place holder. The content from the content page will overwrite it. – mason Feb 06 '17 at 17:24
  • @Ninjutstu - Thanks for the update. No, you can't mark a comment as answer but I added it as an answer. – Igor Feb 06 '17 at 17:43

1 Answers1

1

MasterType <= that should be Reference as per ASP.NET - Accessing Master Page elements form the Content Page ?

<%@ Reference virtualpath="~/Site.master" %> 
Community
  • 1
  • 1
Igor
  • 55,253
  • 10
  • 80
  • 149