Search code examples
c#htmlasp.netrepeater

Make ID the first attribute on input tag while using a repeater


I am using a asp:repeater but there is a tracking technology that needs the first attribute to be the id so it can make a bit by bit comparison.

The problem with this is that the repeater renders the control so that the name is the first attribute and then the type of the input, the id is the third attribute and I need to find a way to make it first.

Has anyone struggled with an issue like this?

<input name="repeater$ctl00$txtField" type="text" id="repeater_ctl00_txtField" class="gray" />

Thanks.


Solution

  • Not too elegant I guess, but using javascript;

    ...get the elements somehow
    
        for (var j = 0; j < elements.length; j++) {
        var attributes = [];
        var attrId = '';
        var i;
        for (i = 0; i < elements[j].attributes.length; i++) {
            if (elements[j].attributes[i].name == 'id')
                attrId = elements[j].attributes[i].value;
            else
                attributes.push({
                    'name': elements[j].attributes[i].name,
                    'value': elements[j].attributes[i].value
                });
        }
        //Remove all attributes
        for (i = 0; i < attributes.length; i++) {
            elements[j].attributes.removeNamedItem(attributes[i]['name']);
        }
        //Add the id
        elements[j].setAttribute('id', attrId);
        //Add the rest
        for (i = 0; i < attributes.length; i++) {
            elements[j].setAttribute(attributes[i]['name'], attributes[i]['value']);
        }
    }
    

    BTW: Attr order does not matter, that tracking technology shoud be aware of it