<p>I am refactoring some CSS on a website. I have been working on, and noticed the absence of traditional HTML IDs in the code. </p>

<p>There is heavy use of <code>CssClass='&amp;hellip;'</code>, or sometimes just <code>class='&amp;hellip;'</code>, but I can't seem to find a way to say id='&hellip;' and not have it swapped out by the server.</p>

<p>Here is an example:</p>

<pre><code>&lt;span id='position_title' runat='server'&gt;Manager&lt;/span&gt;
</code></pre>

<p>When the response comes back from the server, I get:</p>

<pre><code>&lt;span id='$aspnet$crap$here$position_title'&gt;Manager&lt;/span&gt;</code></pre>

<p>Any help here?</p>

[{'answer_id': 65051, 'author': 'Matt Dawdy', 'author_id': 232, 'author_profile': 'https://Stackoverflow.com/users/232', 'pm_score': 0, 'selected': False, 'text': "<p>.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...</p>\n\n<p>ADDED:\nLike 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.</p>\n\n<p>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.</p>\n\n<p>So, with that in mind, is your job of refactoring the CSS getting to be a bit larger in scope?</p>\n"}, {'answer_id': 65082, 'author': 'David Thibault', 'author_id': 5903, 'author_profile': 'https://Stackoverflow.com/users/5903', 'pm_score': 3, 'selected': False, 'text': '<p>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. </p>\n\n<p>If you need to refer to the id in script, you can use the ClientID of the control, like so:</p>\n\n<pre><code>&lt;script type="text/javascript"&gt;\n    var theSpan = document.getElementById(\'&lt;%= position_title.ClientID %&gt;\');\n&lt;/script&gt;\n</code></pre>\n'}, {'answer_id': 65127, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>Ok, I guess the jury is out on this one. </p>\n\n<p>@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. </p>\n\n<p>I'm either left with using classes everywhere, or some garbled looking IDs hardcoded in the css.</p>\n"}, {'answer_id': 65138, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>@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.</p>\n'}, {'answer_id': 65147, 'author': 'B0fh', 'author_id': 9159, 'author_profile': 'https://Stackoverflow.com/users/9159', 'pm_score': 0, 'selected': False, 'text': '<p>If you are accessing the <code>span</code> or whatever tag is giving you problems from the C# or VB code behind, then the <code>runat="server"</code> has to remain and you should use instead <code>&lt;span class="some_class" id="someID"&gt;</code>. If you are not accessing the tag in the code behind, then remove the <code>runat="server"</code>.</p>\n'}, {'answer_id': 65174, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 0, 'selected': False, 'text': '<p>The best thing to do here is give it a unique class name.  </p>\n'}, {'answer_id': 65231, 'author': 'cfeduke', 'author_id': 5645, 'author_profile': 'https://Stackoverflow.com/users/5645', 'pm_score': 0, 'selected': False, 'text': '<p>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.</p>\n\n<p>Not an elegant or easy solution (and it requires a recompile), but it works.</p>\n'}, {'answer_id': 65327, 'author': 'rp.', 'author_id': 2536, 'author_profile': 'https://Stackoverflow.com/users/2536', 'pm_score': 3, 'selected': False, 'text': '<p>Use jQuery to select the element:    </p>\n\n<pre><code>$("span[id$=\'position_title\']")....\n</code></pre>\n\n<p>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.</p>\n\n<p>rp</p>\n'}, {'answer_id': 65586, 'author': 'Joshua Carmody', 'author_id': 8409, 'author_profile': 'https://Stackoverflow.com/users/8409', 'pm_score': 0, 'selected': False, 'text': '<p>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:</p>\n\n<p>1 - Nest spans, one with runat="server", one without:</p>\n\n<pre><code>&lt;style type="text/css"&gt;\n#position_title { // Whatever\n}\n&lt;span id="position_titleserver" runat="server"&gt;&lt;span id="position_title"&gt;Manager&lt;/span&gt;&lt;/span&gt;\n</code></pre>\n\n<p>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...</p>\n\n<pre><code>&lt;style type="text/css"&gt;\n.position_title { font-weight: bold; }\n.foo { color: red; }\n.bar { font-style: italic; }\n&lt;/style&gt;\n&lt;span id="thiswillbemangled" class="foo bar position_title" runat="server"&gt;Manager&lt;/span&gt;\n</code></pre>\n\n<p>...will display this:</p>\n\n<p><b><i>Manager</i></b></p>\n\n<p>3 - Write a Javascript function to fix the IDs after the page loads</p>\n\n<pre><code>function fixIds()\n{\n    var tagList = document.getElementsByTagName("*");\n    for(var i=0;i&lt;tagList.length;i++)\n    {\n        if(tagList[i].id)\n        {\n            if(tagList[i].id.indexOf(\'$\') &gt; -1)\n            {\n                var tempArray = tagList[i].id.split("$");\n                tagList[i].id = tempArray[tempArray.length - 1];\n            }\n        }\n    }\n}\n</code></pre>\n'}, {'answer_id': 66253, 'author': 'NICCAI', 'author_id': 1629400, 'author_profile': 'https://Stackoverflow.com/users/1629400', 'pm_score': 0, 'selected': False, 'text': "<p>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.</p>\n"}, {'answer_id': 66325, 'author': 'Scott Gowell', 'author_id': 6943, 'author_profile': 'https://Stackoverflow.com/users/6943', 'pm_score': 0, 'selected': False, 'text': '<p>Is there a particular reason that you want the controls to be runat="server"?</p>\n\n<p>If so, I second the use of &lt; asp : Literal > . . . </p>\n\n<p>It should do the job for you as you will still be able to edit the data in code behind.</p>\n'}, {'answer_id': 66336, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>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.</p>\n'}, {'answer_id': 72735, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>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.</p>\n\n<p>For example:</p>\n\n<pre><code>[style type="text/css"]\n  #&lt;%= AspNetId.ClientID %&gt; {\n      ... styles go here...\n  }\n[/style]\n\n[script type="text/javascript"]\n   document.getElementById("&lt;%= AspNetId.ClientID %&gt;");\n[/script]\n</code></pre>\n\n<p>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.</p>\n\n<p>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.</p>\n'}, {'answer_id': 112821, 'author': 'Herb Caudill', 'author_id': 239663, 'author_profile': 'https://Stackoverflow.com/users/239663', 'pm_score': 1, 'selected': False, 'text': '<p>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.</p>\n\n<pre><code>  &lt;span id="FooContainer"&gt;\n     &lt;span runat="server" id="Foo" &gt;\n         ......\n     &lt;span&gt;\n  &lt;/span&gt;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/65039', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']