<p>I'm an Information Architect and JavaScript developer by trade nowadays, but recently I've been getting back into back-end coding again.  And, whilst trying to get an HTML prototype integrated and working with our C#-based CMS, I've come to blows with our programmers over the HTML ID attributes being arbitrarily rewritten by .NET for form elements.</p>

<p>I can understand the code-behind reasoning for .NET changing IDs, but the fact you can no longer use IDs when trying to develop e.g. jQuery enhanced interfaces is causing some friction.  What can I do to work around this?</p>

<p>I've tried using the class attribute instead, but that's really crappy, not what it's meant for and doesn't get around that problem of .NET effectively changing rendered source on the fly.  It also means that CSS is less useful now and less efficient to create and maintain.</p>

<p>Any tips or advice greatly appreciated--anything for a few less sleepless nights...</p>

[{'answer_id': 39012, 'author': 'Serhat Ozgel', 'author_id': 31505, 'author_profile': 'https://Stackoverflow.com/users/31505', 'pm_score': 2, 'selected': False, 'text': '<p>You can extend .net controls and make them return actual id\'s when related properties are called.</p>\n\n<p>ClientID is the id attribute and UniqueID is the name attribute of html elements. So when you create a textbox like the following and using this instead of the textbox in framework, you make id and name attributes render as the same as the server-side id.</p>\n\n<pre><code>public class MyTextBox : TextBox\n{\n    public override string ClientID { get { return ID; } }\n    public override string UniqueID { get { return ID; } }\n}\n</code></pre>\n\n<p>To use this new user control, basically register this control as you would do for a custom user control (you can do is in web.config so you won\'t have to do it in all your pages):</p>\n\n<pre><code>&lt;%@ Register Assembly="MyLibrary" NameSpace="MyLibrary.WebControls" TagPrefix="MyPrefix" %&gt;\n</code></pre>\n\n<p>And use it like you would use a text box:</p>\n\n<pre><code>&lt;MyPrefix:MyTextBox ID="sampleTextBox" runat="server" /&gt;\n</code></pre>\n'}, {'answer_id': 39015, 'author': 'Glenn Slaven', 'author_id': 2975, 'author_profile': 'https://Stackoverflow.com/users/2975', 'pm_score': 6, 'selected': True, 'text': '<p>The short answer is no, with webforms the id can always be rewritten depending on the nesting of the element.  You can get access to the id through the ClientID property, so you could set the ids into variables in a script at the end of the page/control then put them into jQuery.  </p>\n\n<p>something like this:</p>\n\n<pre><code>&lt;asp:button id="ImAButton" runat="server"&gt;Click Me&lt;/asp:button&gt;\n\n&lt;script type="text/javascript"&gt;\nvar buttonId = "&lt;%=ImAButton.ClientId%&gt;";\n$("#"+buttonId).bind(\'click\', function() { alert(\'hi); });\n&lt;/script&gt;\n</code></pre>\n\n<p>It\'s a hack I know, but it will work.\n(I should note for the un-initiated, I\'m using the Prototype $ get by id method there)</p>\n'}, {'answer_id': 39019, 'author': 'Jon Galloway', 'author_id': 5, 'author_profile': 'https://Stackoverflow.com/users/5', 'pm_score': 4, 'selected': False, 'text': '<p>One method is to override the ID\'s manually:</p>\n\n<pre><code>public override string UniqueID\n{\n  get { return this.ID; }\n}\npublic override string ClientID\n{\n  get { return this.ID; }\n}\n</code></pre>\n\n<p><a href="http://www.west-wind.com/WebLog/posts/4605.aspx" rel="nofollow noreferrer">Rick Strahl wrote a blog post</a> with some more information on that approach.</p>\n'}, {'answer_id': 39058, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 3, 'selected': False, 'text': "<p>Look at ASP.Net MVC - it addresses the over-kill object hierarchies that ASP.Net generates by default.</p>\n\n<p>This site is written in MVC (I think) - look at it's structure. Were I working on a new project right now I would consider it first</p>\n\n<p>If you're stuck with basic ASP.Net then be careful overriding the ClientID and UniqueID - it tends to break many web controls.</p>\n\n<p>The best way I've found is to pass the unreadable ClientID out to the Javascript.</p>\n"}, {'answer_id': 40143, 'author': 'Jason Bunting', 'author_id': 1790, 'author_profile': 'https://Stackoverflow.com/users/1790', 'pm_score': 1, 'selected': False, 'text': '<p>Personally, I use a set of methods I have developed for bridging the server-side ASP.NET "magic" (I have yet to use the MS MVC stuff yet) and my client-side code because of the munging of the IDs that happens. Here is just one that may or may not prove useful:</p>\n\n<pre><code>public void RegisterControlClientID(Control control)\n{\n   string variableDeclaration = string.Format("var {0} = \\"{1}\\";", control.ID, control.ClientID);\n   ClientScript.RegisterClientScriptBlock(GetType(), control.ID, variableDeclaration, true);\n}\n</code></pre>\n\n<p>So, in your server-side code you simply call this and pass in the instance of a control for which you want to use a friendlier name for. In other words, during <strong>design time</strong>, you may have a textbox with the ID of "m_SomeTextBox" and you want to be able to write your JavaScript using that same name - you would simply call this method in your server-side code:</p>\n\n<pre><code>RegisterControlClientID(m_SomeTextBox);\n</code></pre>\n\n<p>And then on the client the following is rendered:</p>\n\n<pre><code>var m_SomeTextBox = "ctl00_m_ContentPlaceHolder_m_SomeTextBox";\n</code></pre>\n\n<p>That way all of your JavaScript code can be fairly ignorant of what ASP.NET decides to name the variable. Granted, there are some caveats to this, such as when you have multiple instances of a control on a page (because of using multiple instances of user controls that all have an instance of m_SomeTextBox within them, for example), but generally this method may be useful for your most basic needs.</p>\n'}, {'answer_id': 47034, 'author': 'Jesús E. Santos', 'author_id': 4547, 'author_profile': 'https://Stackoverflow.com/users/4547', 'pm_score': 1, 'selected': False, 'text': '<p>What I usually do is create a general function that receives the name of the field. It adds the usual "asp.net" prefix and returns the object.</p>\n\n<pre><code>var elemPrefix = \'ctl00-ContentPlaceHolder-\'; //replace the dashes for underscores\n\nvar o = function(name)\n{    \n    return document.getElementById(elemPrefix + name)\n}\n</code></pre>\n\n<p>With that you can use this kind of calls in jQuery</p>\n\n<pre><code>$(o(\'buttonId\')).bind(\'click\', function() { alert(\'hi); });\n</code></pre>\n'}, {'answer_id': 51695, 'author': 'Ian Oxley', 'author_id': 1904, 'author_profile': 'https://Stackoverflow.com/users/1904', 'pm_score': 0, 'selected': False, 'text': '<p>If you\'re using jQuery then you have loads of <a href="http://docs.jquery.com/Selectors" rel="nofollow noreferrer">CSS selectors and jQuery custome selectors</a> at your disposal to target elements on your page. So rather than picking out a submit button by it\'s id, you could do something like:</p>\n\n<pre><code>$(\'fieldset &gt; input[type="submit"]\').click(function() {...});\n</code></pre>\n'}, {'answer_id': 112815, 'author': 'Herb Caudill', 'author_id': 239663, 'author_profile': 'https://Stackoverflow.com/users/239663', 'pm_score': 1, 'selected': False, 'text': '<p>You definitely don\'t want to hard-code the asp.net-generated ID into your CSS, because it can change if you rearrange things on your page in such a way that your control tree changes. </p>\n\n<p>You\'re right that CSS IDs have their place, so I would ignore the suggestions to just use classes.</p>\n\n<p>The various javascript hacks described here are overkill for a small problem. So is inheriting from a class and overriding the ID property. And it\'s certainly not helpful to suggest switching to MVC when all you want to do is refactor some CSS.</p>\n\n<p>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;div id="DataGridContainer"&gt;\n     &lt;asp:datagrid runat=server id="DataGrid" &gt;\n         ......\n     &lt;asp:datagrid&gt;\n  &lt;/div&gt;\n</code></pre>\n'}, {'answer_id': 1836141, 'author': 'Hogan', 'author_id': 215752, 'author_profile': 'https://Stackoverflow.com/users/215752', 'pm_score': 0, 'selected': False, 'text': '<p>I can see how the .NET system feels less intuitive, but give it a chance.  In my experience it actually ends up creating cleaner code.  Sure</p>\n\n<pre><code>&lt;asp:button id="ImAButton" runat="server"&gt;Click Me&lt;/asp:button&gt;\n\n&lt;script type="text/javascript"&gt;\nvar buttonId = &lt;%=ImAButton.ClientId%&gt;\n$(buttonId).bind(\'click\', function() { alert(\'hi); });\n&lt;/script&gt;\n</code></pre>\n\n<p>works fine.  But this is suffers from not being modular.  What you really want is something like this:</p>\n\n<pre><code>&lt;script type="text/javascript"&gt;\nfunction MakeAClick(inid)\n{\n  $(inid).bind(\'click\', function() { alert(\'hi); });\n}\n&lt;/script&gt;\n</code></pre>\n\n<p>and then later with your code on the java side or the C# side you call MakeAClick.  Of course on the C# side it makes more sense, you just ClientID in there.</p>\n\n<p>Maybe this is the real problem with the code you are reviewing.</p>\n'}, {'answer_id': 35334302, 'author': 'FanManPro', 'author_id': 4214582, 'author_profile': 'https://Stackoverflow.com/users/4214582', 'pm_score': 0, 'selected': False, 'text': '<p>A much better approach would be to use the ClientIDMode and set it to <code>static</code>. You can even set it for a specific page or globally in the web.config file. Then you never have to deal with this issue again and your  JQuery is much cleaner.</p>\n\n<p><strong>Top of page:</strong></p>\n\n<pre><code>&lt;%@ Page Title="" ClientIDMode="Static" Language="C#" CodeBehind="..." Inherits="WebApplication1.WebForm2"  %&gt;\n</code></pre>\n\n<p><strong>On control only:</strong></p>\n\n<pre><code>&lt;asp:Panel runat="server"  ClientIDMode="Static"&gt;&lt;/asp:Panel&gt;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/38998', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3212/']