Solved

How do I find the title / name of the master page of my user contol?

Posted on 2013-01-25
15
287 Views
Last Modified: 2013-03-10
I'd like to hide or show a couple columns on my user control based on which page (master page) is using the control. How, in my OnDataBound event, do I retreive the name of the master page that is using the control? My app is written in ASP .NET 3.5 [C#].
0
Comment
Question by:mikesExpertExchange
  • 8
  • 6
15 Comments
 
LVL 7

Expert Comment

by:tipsybroom
Comment Utility
you could iterate through the parents until you find the desired control, such as with an extension method.

public static Control GetParentOfType(this Control childControl,
                                   Type parentType)
  {
      Control parent = childControl.Parent;
      while(parent.GetType() != parentType)
      {
          parent = parent.Parent;
      }
      if(parent.GetType() == parentType)
            return parent;

     throw new Exception("No control of expected type was found");
  }

Open in new window

0
 
LVL 41

Expert Comment

by:guru_sami
Comment Utility
Did you try this: this.Page.MasterPageFile
0
 
LVL 1

Author Comment

by:mikesExpertExchange
Comment Utility
@guru_sami: then do I access the ".Name" or ".Title" property (in your 2nd suggestion)? I'm oot at my computer now but will be shortly to try this.
0
 
LVL 1

Author Comment

by:mikesExpertExchange
Comment Utility
oot = not
0
 
LVL 41

Expert Comment

by:guru_sami
Comment Utility
Nope...that will be the path like /yoursite/MasterPageName.master
you can simply extract the name from that string.
Something like:
this.Page.MasterPageFile.Split('/').LastOrDefault().Split('.').FirstOrDefault()

Open in new window

0
 
LVL 1

Author Comment

by:mikesExpertExchange
Comment Utility
@ guru_sami: ok I'll try this in a little while.
0
 
LVL 1

Author Comment

by:mikesExpertExchange
Comment Utility
@ guru_sami: That's close, all i got was: "site", any indication on how to drill down to the correct value?
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 41

Expert Comment

by:guru_sami
Comment Utility
You page must be a content page with masterpage.
What's the value you are getting for MasterPageFile?
Add this to test...
Response.Write(this.Page.MasterPageFile);
0
 
LVL 1

Author Comment

by:mikesExpertExchange
Comment Utility
I get: "site/Master"
0
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
Comment Utility
then try this work...

this.Page.MasterPageFile.Split('/').Last();
0
 
LVL 1

Author Comment

by:mikesExpertExchange
Comment Utility
still the same (more or less): "site.master"
0
 
LVL 41

Expert Comment

by:guru_sami
Comment Utility
site.Master or site/Master?
0
 
LVL 1

Assisted Solution

by:mikesExpertExchange
mikesExpertExchange earned 0 total points
Comment Utility
i think i got both. "site/Master" from: this.Page.MasterPageFile and "site.master" from: this.Page.MasterPageFile.Split('/').Last();

this however:

            Page page = HttpContext.Current.Handler as Page;
            pageTitle = page.ToString();

did work
0
 
LVL 41

Expert Comment

by:guru_sami
Comment Utility
But isn't that the page itself?
Or may be I am misunderstanding...
By "master page" do you mean the .aspx Page or the .master MasterPage?
0
 
LVL 1

Author Closing Comment

by:mikesExpertExchange
Comment Utility
thank you
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

728 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