Solved

Can a aspx page pass a parameter to a master page?

Posted on 2008-10-08
9
1,965 Views
Last Modified: 2010-09-13
Hello,
I would like pages that use my Master page to be able to tell the master page whether to load the wide or normal style. Based on this I will show/hide different controls and CSS files.

So can an aspx page pass a parameter to its master page?
Or does it load too late in the page cycle?
private bool isWide;
 
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
 
    public bool IsWide
    {
        get { return isWide; }
        set { isWide = value; }
    }

Open in new window

0
Comment
Question by:uswebpro9
[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
  • 5
  • 3
9 Comments
 

Author Comment

by:uswebpro9
ID: 22667404
Hello,
I'm aware that you can register & show/hide controls and css files in the aspx pages. However I do not want to have a ton of redundant code.
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22667634
i dont know about passing parameters but you definitely can access the content pages controls on the master page
the follwoing link provides u the sample for that
http://www.velocityreviews.com/forums/t114087-access-content-page-controls-from-within-the-master-page.html
0
 
LVL 14

Expert Comment

by:Ramuncikas
ID: 22667694
Hi.

Yes you can

First thing to do is to declare a of what type you MasterPage is. To do so you have to add MasterType declaration right after Page declaration in your content aspx file, like:
<%@ Page ... %>
<%@ MasterType TypeName="MyNamespace.MyMasterPage" %>

Second thing to do is to define a public property on master page (IsWide in your case)

Third thing is to set this property from content page in Page_Load event, like:
this.Master.IsWide = true;

At this point it should be Master page's responsability to load needed controls and styles.

HTH
R
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

Author Comment

by:uswebpro9
ID: 22667697
Right, as I mentioned above. There would be a lot of redundant code if I start accessing the controls on the master page. currently I'm doing this, however it's starting to get messy because I have the same code on 50+ pages now.

It would be cleanest to say this.MasterPage.IsWide = true;

And on the masterPage PageLoad do all the special stuff.

Can I do this some how?
0
 
LVL 14

Expert Comment

by:Ramuncikas
ID: 22667777
I don't know who you just replied to but if you have no luck with my suggestion just please tell.

R
0
 

Author Comment

by:uswebpro9
ID: 22667898
Hey,
The comment above was to ragi0017
Ramuncikas - Thanks! I will try this now.

It might be tricky because I have 5 different masterpages.

In the Global.asax file I set the masterpage and themes based on a setting from a resource file.

0
 

Author Comment

by:uswebpro9
ID: 22667919
Hello Ramuncikas ,
1 question about how to set:
<%@ MasterType TypeName="MyNamespace.MyMasterPage" %>

The master pages has this on the top:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="TechSeminar.master.cs" Inherits="MasterPages_TechSeminar" %>

What do I put for TypeName?
I would prefer to establish this on the aspx.cs code behind page. Can this be done?


thanks!
0
 
LVL 14

Accepted Solution

by:
Ramuncikas earned 500 total points
ID: 22667988
<%@ MasterType TypeName="MasterPages_TechSeminar" %>

Well, actually MasterType declaration is not mandatory. Sure you can always use codebehind.
If you don't set MasterType declaration then in codebehind of your content page you should do something like this:

MasterPages_TechSeminar master = (MasterPages_TechSeminar)this.Master;
master.IsWide = true;

MasterType declaration just ensures that page's Master property returns correct/desired type of master page. If you don't put MasterType declaration then you have to do the above manual type conversion to access you property.

R

0
 

Author Comment

by:uswebpro9
ID: 22671367
Bingo, it worked.

So, an aspx page can send a parameter to it's masterpage.

In this case the parameter hides some user controls.

thanks Ramuncikas!
aspx page:
 
ASP.FrontMasterPage master = (ASP.FrontMasterPage)this.Master;
        master.IsWide = true;
 
 
masterpage cb:
 
public partial class FrontMasterPage : System.Web.UI.MasterPage
{
 
    private bool isWide;
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (isWide)
        {
            headControl1.Visible = false;
        }
    }
 
    public bool IsWide
    {
        get { return isWide; }
        set { isWide = value; }
    }
   
}

Open in new window

0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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!
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 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