Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 303
  • Last Modified:

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

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
Michael Sterling
Asked:
Michael Sterling
  • 8
  • 6
2 Solutions
 
tipsybroomCommented:
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
 
guru_samiCommented:
Did you try this: this.Page.MasterPageFile
0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
@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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
Michael SterlingWeb Applications DeveloperAuthor Commented:
oot = not
0
 
guru_samiCommented:
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
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
@ guru_sami: ok I'll try this in a little while.
0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
@ guru_sami: That's close, all i got was: "site", any indication on how to drill down to the correct value?
0
 
guru_samiCommented:
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
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
I get: "site/Master"
0
 
guru_samiCommented:
then try this work...

this.Page.MasterPageFile.Split('/').Last();
0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
still the same (more or less): "site.master"
0
 
guru_samiCommented:
site.Master or site/Master?
0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
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
 
guru_samiCommented:
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
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now