views:

46

answers:

2

Hello. when i use embedded javascript functions i can get client id of elements with this code

for example : document.getElementById('<%=buttonXXX.ClientID%>' )

but now i am using external javascript file for caching and faster rendering

but this code does not work any more for getting client id's of elements

it gives error

how can i get client ids of elements at external javascript file

using

asp.net 2.0 , netframework 3.5 , c# , iis 7.5

any help is appreciated

thank you

A: 

You could use a class selector. jquery might greatly simplify your life here. So you could apply a special class to the control:

<asp:LinkButton ID="foo" CssClass="foo" runat="server" Text="foo" />

and in your external javascript file once the DOM is ready you could reference the button using a class selector:

$(function() {
    var fooButton = $('.foo');
});
Darin Dimitrov
Darin Dimitrov thanks for answer but unfortunately i am not using jquery :( and do not know how to use. i think i will move my project to the asp.net 4.0 since it does support static ids :)
PokemonCraft
+2  A: 

I can suggest 2 ways.

First way define your variables before call the javascript.

var ButtonXXXID = <%=buttonXXX.ClientID%>
// and now include your javascript and use the variable ButtonXXXID

Second way, in the external javascript file, write your code as:

function oNameCls(ControlId1) {

    this.ControlId1 = ControlId1;

    this.DoYourWork1 = function() {
        // use the control id.
        // this.ControlId1
    }   
}

And call your actions like.

<script>
    // init - create
    var <%=this.ClientID%>MyCls = new oNameCls(<%=Control1.ClientID%>);
    // do your work
    <%=this.ClientID%>MyCls.DoYourWork1();
</script>
Aristos
@PokemonCraft, did you feel this answer 'solves' your problem? If so, click the little green checkmark : ) If not, give us some feedback so we can determine how to help you more.
rlb.usa