Solved

asp.net show hide menu buttons from the master page

Posted on 2011-03-18
20
9,007 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

10 Experts available now in Live!

Get 1:1 Help Now