.Net: Give Me My ID Back!

Working as a designer on a .Net project must be hellish, I thought yesterday as I was struggling with label tags. The “official” web-designer was lying sick in his warm bed and I had to do the job on my own. And that’s how I came to design a form with .Net.

The label HTML tag is handy. When used with the for attribute, the normal behaviour is to position the cursor in the associated field when clicking onto the label. The for attribute must contain the id of the associated field to get this behaviour. But the thing is that .Net rewrites the id of the fields to meet its purposes: thus, the id for the text field you initially called “MyTextField” becomes something like “MyWonderfulUserControl_MyTextField”. And the label no longer works.

The same problem occurs when you want to work with JavaScript. When using getElementById, there’s no way you’re going to find your element since its id has changed. The only fix I came up with was to get the elements with getElementsByTagName, and then check if the name contains the original id:

var label = document.getElementsByTagName('label');
for (i = 0 ; i < label.length ; i++)
{
    var tmp = label[i];
    if (tmp.id.indexOf('MonLabel') > -1 ) {
     // do something, silly billy...
    }
}

That being said, I’m no .Net expert, there might be something I am missing there. At the end of the day, the thought of .Net playing with ids leaves me somewhat bemused.¶

 
---

Commenting is closed for this article.

---