Unnikrishnan.S
Unnikrishnan.S

Reputation: 185

How to hide a div which is in master page using asp.net?

Iam tried to hide a div which is placed in master page,but i got an error like this "Object reference not set to an instance of an object".

My codes

<div runat="server" id="cnms">
 <a href="Cinemas.aspx">Cinemas</a>
</div>

public partial class Default : System.Web.UI.Page
 {
   protected void Page_Load(object sender, EventArgs e)
     {
        AdminMaster admns = new AdminMaster();//This si my admin page
        admns.FindControl("cnms").Visible = false;//I got error here
     }
 }

What went wrong for me?any solution?

Upvotes: 0

Views: 2672

Answers (1)

Brian Mains
Brian Mains

Reputation: 50728

The approach you are using is instantiating a new instance of the masterpage, which when you do that it all of the control references are null. You need to use the existing master page instance, using the Page.Master property like the following:

 protected void Page_Load(object sender, EventArgs e)
 {
    AdminMaster admns = (AdminMaster)Page.Master; //This si my admin page
    admns.FindControl("cnms").Visible = false;//I got error here
 }

Upvotes: 2

Related Questions