views:

245

answers:

5

I am not a .Net guy but trying to get update an existing .net site. Trying to set up a unique id for the body tag even though I am using a master page.

<body id="<% page_name %>">

Obviously the above example doesn't work but not quite sure if there is a function that returns the file name that can be easily added to the masterpage so it give each page's body tag a unique "id" based on the page name.

I would perfer not having to set some variable up on each code behind page but just once on the master page.

Could be going about this the completely wrong way. Using PHP I have done this a number of times.

Basically I am trying to get a unique body id for each of the pages of the site. This will allow me to use CSS to style page specific layout items.

A: 

A quick Google search led me to this page. Maybe Request.Url is what you need?

varzan
System.IO.Path.GetFileName(Request.PhysicalPath) is a way to get the filename.
RichardOD
anyway to get that pathname to the master page without using javascript
Ben4Himv
A: 

You may want to have each page that is using your masterpage, add some onload javascript to the page (startup script block) and that JS function will set the body's id.

Have your setBodyId(txt) function defined in a js file that every page includes. Or even put it in the masterpage. Then do something like this (untested):

In the OnPreRender event in the codebehind:

ScriptManager.RegisterStartupScript(this, typeof(MyPage), "MyPage_Init", "setBodyId('MyPageaspx');", true);

Then your JS function will be like this (untested):

function setBodyId(txt){
 document.body.id = txt;
}

Note I took the dot out of the page name to avoid periods in the body id.

Sherri
A: 

If you want a solution contained in the Masterpage, add an onload function call to the body tag.

<body onload="setBodyId()">

Define this JS function in the masterpage. Something like this:

function setBodyId(){    
  var sPath = window.location.pathname;
  var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
  document.body.id=sPage;
}
Sherri
I tested this and it works.
Sherri
+2  A: 

I've done exactly what you're talking about. This is how I did it.

What you have in the body tag is pretty close. This is what I've used in the masterpage's markup, it's a code behind method.

<body id="<%=SetupPageName()%>">

In the masterpage code behind, you need this using statement:

using System.IO;

Basically what this does is gets the file name of the page that you will be accessing during runtime.

The following methods are used to grab the file name of the page, and then assign that file name as a unique id to the generated page.

 private string pageName = string.Empty;

protected string SetupPageName()
{
    string pageName = GetPageName();
    return pageName;
}

private string GetPageName()
{
    pageName = Path.GetFileNameWithoutExtension(Request.FilePath);
    if (pageName == "default")
    {
        pageName = "Default";
    }
    return pageName;
}

Now you should be able to access that body ID just like you would with any other type of ID. I've used this to enable navigation highlighting via a stylesheet. There are a few more steps involved with that, but you can generally find those answers on google.

Good luck, and hope this helps.

Chris
This should do it. Thanks!
Ben4Himv
A: 

I am using this:

<body id="<%= Path.GetFileNameWithoutExtension(Request.FilePath) %>">

but it's not friendly to URL rewriting.

Mark Richman