+1  A: 

.Net will always replace your id values with some mangled (every so slightly predictable, but still don't count on it) value. Do you really NEED to have that id runat=server? If you don't put in runat=server, then it won't mangle it...

ADDED: Like leddt said, you can reference the span (or any runat=server with an id) by using ClientID, but I don't think that works in CSS.

But I think that you have a larger problem if your CSS is using ID based selectors. You can't re-use an ID. You can't have multiple items on the same page with the same ID. .Net will complain about that.

So, with that in mind, is your job of refactoring the CSS getting to be a bit larger in scope?

Matt Dawdy
+4  A: 

The 'crap' placed in front of the id is related to the container(s) of the control and there is no way (as far as I know) to prevent this behavior, other than not putting it in any container.

If you need to refer to the id in script, you can use the ClientID of the control, like so:

<script type="text/javascript">
    var theSpan = document.getElementById('<%= position_title.ClientID %>');
</script>
David Thibault
Note that this won't fix your CSS problem as you can't use this technique in an external CSS file. If you REALLY need to use the ID in your CSS, you should try to find a way to not mark the element as runat="server". Maybe with an <asp:Literal ... /> ?
David Thibault
A: 

Ok, I guess the jury is out on this one.

@leddt, I already knew that the 'crap' was the containers surrounding it, but I thought maybe Microsoft would have left a backdoor to leave the ID alone. Regenerating CSS files on every use by including ClientIDs would be a horrible idea.

I'm either left with using classes everywhere, or some garbled looking IDs hardcoded in the css.

If you REALLY need to use the ID in your CSS, you should try to find a way to not mark the element as runat="server". Maybe with an <asp:Literal ... /> ?
David Thibault
<span id="whatever"> <asp:Literal id="whatever_content" runat="server" /></span>You can set the content of the literal in code, and it won't be surrounded by any extra tag.
David Thibault
A: 

@Matt Dawdy: There are some great uses for IDs in CSS, primarily when you want to style an element that you know only appears once in either the website or a page, such as a logout button or masthead.

@Jared. My suggestion didn't come through like I meant it too -- yes, there are some very valid reasons for doing what you suggest. I meant to say that .Net doesn't really agree.Good luck!
Matt Dawdy
A: 

If you are accessing the 'span' or whatever tag is giving you problems from the C# or VB code behind, then the runat="server" has to remain and you should use instead <span class="some_class" id="someID">. If you are not accessing the tag in the code behind, then remove the runat="server".

B0fh
A: 

The best thing to do here is give it a unique class name.

Joel Coehoorn
A: 

You're likely going to have to remove the runat="server" from the span and then place a within the span so you can stylize the span and still have the dynamic internal content.

Not an elegant or easy solution (and it requires a recompile), but it works.

cfeduke
+5  A: 

Use jQuery to select the element:

$("span[id$='position_title']")....

jQuery's flexible selectors, especially its 'begins with'/'ends with selectors' (the 'end with' selector is shown above, provide a great way around ASP.NET's dom id munge.

rp

rp
A: 

I don't know of a way to stop .NET from mangling the ID, but I can think of a couple ways to work around it:

1 - Nest spans, one with runat="server", one without:

<style type="text/css">
#position_title { // Whatever
}
<span id="position_titleserver" runat="server"><span id="position_title">Manager</span></span>

2 - As Joel Coehoorn suggested, use a unique class name instead. Already using the class for something? Doesn't matter, you can use more than 1! This...

<style type="text/css">
.position_title { font-weight: bold; }
.foo { color: red; }
.bar { font-style: italic; }
</style>
<span id="thiswillbemangled" class="foo bar position_title" runat="server">Manager</span>

...will display this:

Manager

3 - Write a Javascript function to fix the IDs after the page loads

function fixIds()
{
    var tagList = document.getElementsByTagName("*");
    for(var i=0;i<tagList.length;i++)
    {
        if(tagList[i].id)
        {
            if(tagList[i].id.indexOf('$') > -1)
            {
                var tempArray = tagList[i].id.split("$");
                tagList[i].id = tempArray[tempArray.length - 1];
            }
        }
    }
}
Joshua Carmody
A: 

If you're fearing classitus, try using an id on a parent or child selector that contains the element that you wish to style. This parent element should NOT have the runat server applied. Simply put, it's a good idea to plan your structural containers to not run code behind (ie. no runat), that way you can access major portions of your application/site using non-altered IDs. If it's too late to do so, add a wrapper div/span or use the class solution as mentioned.

NICCAI
A: 

Is there a particular reason that you want the controls to be runat="server"?

If so, I second the use of < asp : Literal > . . .

It should do the job for you as you will still be able to edit the data in code behind.

Penguinix
A: 

I usually make my own control that extends WebControl or HtmlGenericControl, and I override ClientID - returning the ID property instead of the generated ClientID. This will cause any transformation that .NET does to the ClientID because of naming containers to be reverted back to the original id that you specified in tag markup. This is great if you are using client side libraries like jQuery and need predictable unique ids, but tough if you rely on viewstate for anything server-side.

+1  A: 

You can embed your CSS within the page, sprinkled with some server tags to overcome the problem. At runtime the code blocks will be replaced with the ASP.NET generated IDs.

For example:

[style type="text/css"] #<%= AspNetId.ClientID %> { ... styles go here... } [/style]

[script type="text/javascript"] document.getElementById("<%= AspNetId.ClientID %>"); [/script]

You could go a bit further and have some code files that generate CSS too, if you wanted to have your CSS contained within a separate file.

Also, I may be jumping the gun a bit here, but you could use the ASP.NET MVC stuff (not yet officially released as of this writing) which gets away from the Web Forms and gives you total control over the markup generated.

A: 

I'm sure this has been asked here at least once before. The term is 'munging'. Rich Strahl wrote a post about it in 2006 as well.

CarmineSantini
A: 

Most of the fixes suggested her are overkill for a very simple problem. Just have separate divs and spans that you target with CSS. Don't target the ASP.NET controls directly if you want to use IDs.

  <span id="FooContainer">
     <span runat="server" id="Foo" >
         ......
     <span>
  </span>
Herb Caudill