Solved

asp.net show hide menu buttons from the master page

Posted on 2011-03-18
20
9,195 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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 …
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

776 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