Solved

asp.net show hide menu buttons from the master page

Posted on 2011-03-18
20
9,512 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

724 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