tags:

views:

211

answers:

2

I am struggling to find a way of implementing a tab style menu system in ASP.net. The tabs look great from the HTML/CSS standpoint and are implemeted in the Master page so I don't have access to the body tag from the content page. what I am trying to work out is how to vary the "active tab" based upon the page the user is accessing.

I can think of several ways of doing this:

  1. Dynamicly generate the HTML at each page load
  2. Manipulate the list items in the Page_Load
  3. Use jQuery to manipulate the selected tab class

I have an additional problem which seems to make some solutions unsuitable; one tab may incoporate several different page names, so for example the Courses tab should be selected for both Default.aspx and CourseDetail.aspx.

None of these seem particuarly elegant, I get the feeling that there is a better way of doing this in ASP.net but I don't know what I am looking (or searching) for.

HTML:

<div id="tabs">
    <ul>
        <li class="selectedtab"><a href="/">Courses</a></li>
        <li><a href="#">My Courses</a></li>
        <li><a href="#">Administration</a></li>
    </ul>
</div>

CSS:

#head ul 
{
    list-style: none;
    padding:0;
    margin:0;
}

#head li 
{
    display: inline;
    border: solid;
    border-width: 1px 1px 0 1px;
    margin: 0 0.5em 0 0;
    background: #EEE;
    font-size: large;
    font-weight: bold;
    padding-top: 0.3em;
}

#head li a 
{
    padding: 0 1em;
    text-decoration: none;
    color: Black;
}

#head .selectedtab
{
    padding-bottom: 1px; 
    background: white;
    border-bottom: 1px solid white;
}

#tabs
{
    text-align: right;
    width: 100%;
    border-bottom: 1px solid;
}
+1  A: 

The way I've always done this in the past is to give the body a class, then each tab a class (or id), then use those in the CSS.

<!-- the tabs now have a class you can use -->
<div id="tabs">
    <ul>
        <li class="tabone"><a href="/">Courses</a></li>
        <li class="tabtwo"><a href="#">My Courses</a></li>
        <li class="tabtre"><a href="#">Administration</a></li>
    </ul>
</div>

first page

<body class="tabonepage">...

secondpage

<body class="tabtwopage">...

css - this gets a bit verbose, but it's localised to one spot. As long as you don't have 493 tabs, you'll be golden

.tabonepage .tabone,
.tabtwopage .tabtwo,
.tabtrepage .tabtre,
{
    padding-bottom: 1px; 
    background: white;
    border-bottom: 1px solid white;
}


edit as for how I've gotten the class in...

This is where I hang my head in shame. I usually put a property on my main master called BodyClass, then put a bit of code in OnPreRender that sets said property, and put an expression hole into the main master. It's not real pretty, but I live with it, the benefits of having a body class far outweigh the codebehind in my eyes :-) There's probably a way that doesn't involve codebehind, I've never really investigated it fully. I get the feeling MVC would help.

MainMaster.Master:

<body class='<%=BodyClass%>'>

MainMaster.Master.cs

public string BodyClass{ get; set; }

everyotherpage.aspx (I can't remember if this was needed, I think it's so you have strongly typed access to the BodyClass property in the codebehind)

<%@ MasterType VirtualPath="~/MasterPages/MainMaster.Master" %>

everyotherpage.aspx.cs

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        this.Master.BodyClass = "areamydetails";
    }
Dan F
OK, I like that - any idea how to implement it in ASP.net. I am using master pages so I don't appear to have access to change the CssClass of the body element.
Richard Slater
+1  A: 

I think that switching the CSS class of the DIV or control based on the current location is a considerably elegant way of doing it. If you link the current page being accessed to the ASP.NET Navigation API (using a Sitemap), you should be doing great!

Cerebrus
Yeah, using SiteMap is probably a good idea.
Noldorin