Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

asp.net show hide menu buttons from the master page

Posted on 2011-03-18
20
Medium Priority
?
9,776 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 2000 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

636 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