views:

57

answers:

3

Hi,

I'm curious to know that using .net 2.0 with a master page if there is a way that I can pick up what page I am on so that i can use it to style a tab?

My master page has a nav bar on it, and what I wan to do is:

If the user is, say on the contact page, that the tab for the contact page would be a different color, can this be achieved. I have seen some examples that don't use master pages and of course you can use the encapsulating body tag to signify where you are but this isn't available with a masterpage.

Thanks R.

+1  A: 

If you want to change the content on the masterpage from the page (i.e. change the tab color) you should:

In the masterpage, publicly expose a property or method that will change the color of the tab.

i.e.:

public void changecolor(string PageName, string Color){
    switch(PageName){
       case "home":
           this.TabHome.Color=Color;
    }
}

Then put a directive at the top of the aspx page with the masterpage path. Like such:

 <%@ MasterType VirtualPath="~/Site.master" %>

Once this is done, from the codebehind, you can access the masterpage and see its exposed method, then just call this and you're done.

protected void Page_Init(object sender, EventArgs e){
    Master.changecolor("home", "red"); 
}

this way, you won't have to parse pagenames and deal with the maintenance that comes when you try to change the name of the page etc. you will also limit your case statement to the number of tabs, and not the number of pages in your site.

Russ Bradberry
@Russ: I see that your method is triggering the tab from the page instead of from the masterpage. A different approach but works well too. `+1` Though it will be tight coupling between the every single page and the masterpage, it might be simpler to implement depending on the scenario.
o.k.w
Russ Bradberry
@Russ: Totally agree, so only he knows the answer! Haha... how ironic yea?
o.k.w
+1  A: 

MasterPage though the name sound otherwise behaves like a child to a page that uses it.

Think of it as a UserControl to a page. You can actually access to the Page instance and it's Request property.

Here's an example on how you can use it

switch(Request.Path){
  case "/page1/aspx":
    //dosomething to your tabs
  break:
  case "/page1/aspx":
    //dosomething to your tabs
  break:
  .
  .
  .
  default:
    //dosomething else
  .
  .
  .
}
o.k.w
what happens if he has 100 pages? or dynamic URL rewriting?
Russ Bradberry
@Russ, yes, it should be `switch` instead of `select`, what was I thinking? :P Anyway my code is just an example, it can be using Page.Title or using a lookup if there's one.
o.k.w
That's very well understood, if he is using a database to rewrite or has a list of pages, then this will actually be a very efficient way of doing this, +1.
Russ Bradberry
+1  A: 

Create the following method in your masterpage (or helper class) and then add a reference to it in your Page_Load method in the masterpage:

public string GetCurrentPageName() 
{ 
    Uri uri = Request.Url; 
    string[] uriSegments = uri.Segments; 

    string pageName = "";

    if( 0 < uriSegments.Length ) 
    { 
        pageName = uriSegments.Last(); 
    } 

    return pageName;

}

}

That should give you the current filename - you might want to strip out the ".aspx" part of the filename also. I haven't tested this with a QueryString yet so not sure if Last() still returns the filename in that case.

If your tabs are asp.net controls, you can use FindControl() to find the tab - you'll need to match your tab ids with your page names of course. Once you have the control you can add a "selected" style in code-behind.

NickGPS
you don't see any possible performance issues in using System.IO.FileInfo? Plus using System.IO at the web layer is just plain dirty. It would probaly get better performance and cleaner results if you use the uri segments and return the last index, it should give the same results.
Russ Bradberry
For a normal web app I would agree it's not appropriate when the httpContext can provide the same result. We had to use FileInfo on a recent CMS project when the name of the template file wasn't used for the URI, but was used for dynamic styling.
NickGPS
Code updated now to use the Request object instead of FileInfo.
NickGPS
that is much cleaner, though as you mentioned in your comment, system.io.fileinfo would work for applications that run solely off default.aspx and load a different control for each page. I've seen this in applications like "YetAnotherForum"
Russ Bradberry