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#].
LVL 1
Michael SterlingWeb Applications DeveloperAsked:
Who is Participating?
 
guru_samiConnect With a Mentor Commented:
then try this work...

this.Page.MasterPageFile.Split('/').Last();
0
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
still the same (more or less): "site.master"
0
 
guru_samiCommented:
site.Master or site/Master?
0
 
Michael SterlingConnect With a Mentor Web 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
All Courses

From novice to tech pro — start learning today.