views:

925

answers:

9

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.

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?

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.

Any tips or advice greatly appreciated--anything for a few less sleepless nights...

+4  A: 

You can extend .net controls and make them return actual id's when related properties are called.

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.

public class MyTextBox : TextBox
{
    public override string ClientID { get { return ID; } }
    public override string UniqueID { get { return ID; } }
}

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):

<%@ Register Assembly="MyLibrary" NameSpace="MyLibrary.WebControls" TagPrefix="MyPrefix" %>

And use it like you would use a text box:

<MyPrefix:MyTextBox ID="sampleTextBox" runat="server" />
Serhat Özgel
One thing you'll also need is adapters for listbox controls, and a .browser file
Chris S
+21  A: 

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.

something like this:

<asp:button id="ImAButton" runat="server">Click Me</asp:button>

<script type="text/javascript">
var buttonId = <%=ImAButton.ClientId%>
$(buttonId).bind('click', function() { alert('hi); });
</script>

It's a hack I know, but it will work. (I should note for the un-initiated, I'm using the Prototype $ get by id method there)

Glenn Slaven
You'll need quotes around that server tag of course. And I don't see why this is a hack. It's a legitimate technique that I use all the time. That's what things like server tags and ClientID are for.
harpo
I guess it just feels like a hack because it's messy. I'd much rather their be a static, predictable id on the control that javascript can talk to
Glenn Slaven
I also use this approach all the time. I usually declare all of my "dynamic" variables in the head tag (on standalone pages) or in an asp:Content control that writes to an asp:ContentPlaceHolder in the head tag of a master. That at least keeps all the variables containing dynamic IDs in one spot.
muloh
+12  A: 

One method is to override the ID's manually:

public override string UniqueID
{
  get { return this.ID; }
}
public override string ClientID
{
  get { return this.ID; }
}

Rick Strahl wrote a blog post with some more information on that approach.

Jon Galloway
+7  A: 

Look at ASP.Net MVC - it addresses the over-kill object hierarchies that ASP.Net generates by default.

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

If you're stuck with basic ASP.Net then be careful overriding the ClientID and UniqueID - it tends to break many web controls.

The best way I've found is to pass the unreadable ClientID out to the Javascript.

Keith
+1  A: 

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:

public void RegisterControlClientID(Control control)
{
   string variableDeclaration = string.Format("var {0} = \"{1}\";", control.ID, control.ClientID);
   ClientScript.RegisterClientScriptBlock(GetType(), control.ID, variableDeclaration, true);
}

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 design time, 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:

RegisterControlClientID(m_SomeTextBox);

And then on the client the following is rendered:

var m_SomeTextBox = "ctl00_m_ContentPlaceHolder_m_SomeTextBox";

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.

Jason Bunting
+1  A: 

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.

var elemPrefix = 'ctl00-ContentPlaceHolder-'; //replace the dashes for underscores

var o = function(name)
{    
    return document.getElementById(elemPrefix + name)
}

With that you can use this kind of calls in jQuery

$(o('buttonId')).bind('click', function() { alert('hi); });
jeds
A: 

If you're using jQuery then you have loads of CSS selectors and jQuery custome selectors 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:

$('fieldset > input[type="submit"]').click(function() {...});
Ian Oxley
+1  A: 

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.

You're right that CSS IDs have their place, so I would ignore the suggestions to just use classes.

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.

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.

  <div id="DataGridContainer">
     <asp:datagrid runat=server id="DataGrid" >
         ......
     <asp:datagrid>
  </div>
Herb Caudill
A: 

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

<asp:button id="ImAButton" runat="server">Click Me</asp:button>

<script type="text/javascript">
var buttonId = <%=ImAButton.ClientId%>
$(buttonId).bind('click', function() { alert('hi); });
</script>

works fine. But this is suffers from not being modular. What you really want is something like this:

<script type="text/javascript">
function MakeAClick(inid)
{
  $(inid).bind('click', function() { alert('hi); });
}
</script>

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.

Maybe this is the real problem with the code you are reviewing.

Hogan