Solved

asp.net show hide menu buttons from the master page

Posted on 2011-03-18
20
9,116 Views
Last Modified: 2012-05-11
I'm trying to show/hide buttons from the master page depending on user access level.
In one of the forums there was a solution for it, I tried but it didn't work. Not sure what I was missing.

Thanks for your help.

In the example below, the salesSummary and actionNotes both give NULL objects..

site.master page:
:
<ul>
<li runat="server" id="salesSummary"><ahref="SalesSummary.aspx">Sales Summary</a></li>
<li runat="server" id="actionNotes"><a href="Notes.aspx">Action Notes</a></li>


Code behind:

protected System.Web.UI.HtmlControls.HtmlGenericControl salesSummary;
protected System.Web.UI.HtmlControls.HtmlGenericControl actionNotes;
 
protected void Page_Load(object sender, EventArgs e)
        {
            //If the menu items should be shown, do this:
            salesSummary.Visible = true;
            actionNotes.Visible = true;
 
            //If the menu items should not be shown, do this:
            salesSummary.Visible = false;
            actionNotes.Visible = false;
       
            if (!Page.IsPostBack)
            {
                //Do whatever you want if it's not a postback
            }
        }
0
Comment
Question by:lionkin
  • 13
  • 7
20 Comments
 
LVL 2

Expert Comment

by:tonykachappilly
ID: 35166484
if ur buttons salesSummary. and actionNote are in the master page and u are calling those from the content page it will give null.
you can either call it from master page cs file directly
or to get it from content page
HtmlGenericControl tb = ( HtmlGenericControl ) Master.FindControl("salesSummary");
tb.Visible = false;
0
 

Author Comment

by:lionkin
ID: 35166800
( HtmlGenericControl ) Master.FindControl("salesSummary");
gives me the error: An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Master.get'

<div id='menu_bar'>
                <div id='navcontainer'>
                  <ul id='navlist' runat='server'>
                      <li runat="server" id="salesSummary"><ahref="SalesSummary.aspx">Sales Summary</a></li>
                     <li runat="server" id="actionNotes"><a href="Notes.aspx">Action Notes</a></li>

0
 

Author Comment

by:lionkin
ID: 35166834
I have included the following libraries in the file:

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;
0
 
LVL 2

Expert Comment

by:tonykachappilly
ID: 35166858
r u having the code int he content page.?
0
 
LVL 2

Expert Comment

by:tonykachappilly
ID: 35166872
put runat="server" for master body ..:)
0
 

Author Comment

by:lionkin
ID: 35166897
yes I have runat="server" in the master page, and the code above was defined in SalesSummary.aspx.cs
0
 
LVL 2

Expert Comment

by:tonykachappilly
ID: 35166918
u mean <body id="Body" runat="server">
still error..?
can u post the error
0
 
LVL 2

Expert Comment

by:tonykachappilly
ID: 35166926
as well as the code
so i can help u
0
 

Author Comment

by:lionkin
ID: 35167404
Please find the code below. Can you identify the problem please?
Thanks for your help.


master page:
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="SiteMaster.master.cs" Inherits="MyApp.SiteMaster" %>

<!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">
    <link href="Styles/style.css" rel="stylesheet" type="text/css" />
    <title></title>
   
</head>
<body>
    <form id="form1" runat="server">
      <div id='page_wrapper'>
          <div id='page_header'>
                <h1>MySite.Net</h1>
          </div>
       
          <div id='menu_bar'>
                <div id='navcontainer'>
                  <ul id='navlist' runat='server'>
                     <li runat="server" id="salesSummary"><ahref="SalesSummary.aspx">Sales Summary</a></li>
                     <li runat="server" id="actionNotes"><a href="Notes.aspx">Action Notes</a></li>
                  </ul>
             </div>
          </div>
     </div>
   </form>
</body>
</html>


SalesSummary.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 MotherEarthDev
{
    public partial class Home : System.Web.UI.Page
    {
        protected HtmlGenericControl salesSummary= (HtmlGenericControl)Master.FindControl("salesSummary");
        protected HtmlGenericControl actionNotes= (HtmlGenericControl)Master.FindControl("actionNotes");
       

        protected void Page_Load(object sender, EventArgs e)
        {
            if (salesSummary!= null)
                salesSummary.Visible = true;
            if (actionNotes!= null)
                actionNotes.Visible = false;

            if (!Page.IsPostBack)
            {
            }

        }
    }
}
0
 
LVL 2

Expert Comment

by:tonykachappilly
ID: 35167437
Please put
<body runat="server"> in your master page:
instead for
<body>

your issue will be solved
:)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:tonykachappilly
ID: 35167457
<body runat="server>"

Open in new window


check that .. your body tag don't have the runat attribute
0
 

Author Comment

by:lionkin
ID: 35167522
I see. I have added <body runat="server"> but the error still persists.
0
 
LVL 2

Expert Comment

by:tonykachappilly
ID: 35167588
Can u post your complete error
0
 

Author Comment

by:lionkin
ID: 35167638
The errors were found at "Master"

 protected HtmlGenericControl salesSummary= (HtmlGenericControl)Master.FindControl("salesSummary");
        protected HtmlGenericControl actionNotes= (HtmlGenericControl)Master.FindControl("actionNotes");


Error      1      An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Master.get'      G:\MyApp\MyApp\SalesSummary.aspx.cs      15      67      MyApp

Error      2      An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Master.get'      G:\MyApp\MyApp\SalesSummary.aspx.cs      16      70      MyApp
0
 
LVL 2

Expert Comment

by:tonykachappilly
ID: 35167663
Can u remove the Keyword protected from your code
and use
HtmlGenericControl actionNotes= (HtmlGenericControl)Master.FindControl("actionNotes")

Open in new window

0
 
LVL 2

Expert Comment

by:tonykachappilly
ID: 35167709
sorry the problem is the place where you have the code

just rearrange your code

namespace MotherEarthDev
{
    public partial class Home : System.Web.UI.Page
    {
        
        

        protected void Page_Load(object sender, EventArgs e)
        {
protected HtmlGenericControl salesSummary= (HtmlGenericControl)Master.FindControl("salesSummary");
        protected HtmlGenericControl actionNotes= (HtmlGenericControl)Master.FindControl("actionNotes");
            if (salesSummary!= null)
                salesSummary.Visible = true;
            if (actionNotes!= null)
                actionNotes.Visible = false;

            if (!Page.IsPostBack)
            {
            }

        }
    }
}

Open in new window

0
 
LVL 2

Expert Comment

by:tonykachappilly
ID: 35167721
please have your declaration part within the page load
0
 
LVL 2

Accepted Solution

by:
tonykachappilly earned 500 total points
ID: 35167735
namespace MotherEarthDev
{
    public partial class Home : System.Web.UI.Page
    {
        
        

        protected void Page_Load(object sender, EventArgs e)
        {
           HtmlGenericControl salesSummary= (HtmlGenericControl)Master.FindControl("salesSummary");
            HtmlGenericControl actionNotes= (HtmlGenericControl)Master.FindControl("actionNotes");
            if (salesSummary!= null)
                salesSummary.Visible = true;
            if (actionNotes!= null)
                actionNotes.Visible = false;

            if (!Page.IsPostBack)
            {
            }

        }
    }
}

Open in new window


Copy paste this code it should work fine
:)
0
 

Author Comment

by:lionkin
ID: 35167874
It works great. Thanks very much :)
0
 
LVL 2

Expert Comment

by:tonykachappilly
ID: 35168204
u r welcome :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now