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

x
?
Solved

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

Posted on 2013-01-25
15
Medium Priority
?
299 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:Michael Sterling
[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
  • 8
  • 6
15 Comments
 
LVL 7

Expert Comment

by:tipsybroom
ID: 38820733
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
ID: 38820842
Did you try this: this.Page.MasterPageFile
0
 
LVL 1

Author Comment

by:Michael Sterling
ID: 38820920
@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
Technology Partners: 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 1

Author Comment

by:Michael Sterling
ID: 38820923
oot = not
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 38820934
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:Michael Sterling
ID: 38820975
@ guru_sami: ok I'll try this in a little while.
0
 
LVL 1

Author Comment

by:Michael Sterling
ID: 38821305
@ guru_sami: That's close, all i got was: "site", any indication on how to drill down to the correct value?
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 38821312
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:Michael Sterling
ID: 38821318
I get: "site/Master"
0
 
LVL 41

Accepted Solution

by:
guru_sami earned 2000 total points
ID: 38821330
then try this work...

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

Author Comment

by:Michael Sterling
ID: 38821336
still the same (more or less): "site.master"
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 38821358
site.Master or site/Master?
0
 
LVL 1

Assisted Solution

by:Michael Sterling
Michael Sterling earned 0 total points
ID: 38821368
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
ID: 38821392
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:Michael Sterling
ID: 38970873
thank you
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
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…

610 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