I have a parent master page (Master.Master) and child master page (Child.Master). The Child.Master inherits Master.Master master page file. Now in the Child.Master i want to set the visibility of Div (whose ID is Div1) to false, for which i'm using the following code:
protected void Page_Load(object sender, EventA开发者_如何转开发rgs e)
{
this.FindControl("Div1").Visible = false;
}
Here is the code in the Child Master Page file:
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Webstore.Master.cs" Inherits="WebStore.WebStoreMaster" MasterPageFile="~/Login.Master" %>
<asp:Content ID="UserMaster" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
<div id="Div1" runat="server">
<div id="Sidebar" runat="server" style="float: left; margin-top: 100px; margin-right: 20px;">
</div>
</div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</asp:Content>
The compiler is giving me the following error:
Object reference not set to an instance of an object.
Can someone explain why is it happening so ??
Thanks in anticipation
EDIT:
In this case, if the div is a top level element, and you are in the page_load of the child master page in which the div resides, you should just be able to do
Div1.Visible = false;
Why not use a Panel control?
you should say
this.Master.FindControl("Div1").Visible = false;
Try setting @MasterType in Content Pages and in Child Master Pages. Below are some reference links
http://msdn.microsoft.com/en-us/library/ms228274.aspx
http://dotnet.dzone.com/news/back-basics-%E2%80%93-using-mastertype?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+zones%2Fdotnet+%28.NET+Zone%29
http://dotnetslackers.com/community/blogs/haissam/archive/2008/02/11/mastertype-directive-in-content-page.aspx
Thanks
Master page is loaded is after page_load. Therefore when you attempt to address the master page during page_load it's properties and methods are not yet available. Move this down in the page life cycle. ASP.NET Page Life cycle, Another SO answer on Masterpage/page life cycle. the child master is loaded during the page_load, and parent master is loaded during the child master page_load.
精彩评论