14

When I use embedded javascript functions I can get client id of elements with this code:

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

But now I am using external javascript file for caching and faster rendering and this code does not work any more for getting client id's of elements, it gives error.

How can I get client id's of elements at external javascript file using asp.net 2.0 , netframework 3.5 , c# , iis 7.5

ann
  • 576
  • 1
  • 10
  • 19
Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342

5 Answers5

32

I can suggest 2 ways.

First way

define your variables before call the javascript, inside the .aspx file that can be compiled.

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>

calling the action this way you prevent overwrite one action from one control with the same action from other controls on the same page.

Aristos
  • 66,005
  • 16
  • 114
  • 150
  • @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 Oct 27 '10 at 23:50
5

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
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    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 :) – Furkan Gözükara Oct 02 '10 at 14:39
  • Hey Darin, a few years have gone by since you posted this, but thank you anyways. It was a great, clean, elegant way to use class selectors instead of element id's to apply input masks to various elements in my website. – Bryan Dec 12 '14 at 21:28
1

in script file (test.js)

function test(ControlID1) {
  var controlId = document.getElementById(ControlID1);
  controlId.onchange = function () {        
    alert(controlId.value);
    }
  }

in .aspx file

<script type="text/javascript">
   var callTest = test('<%=txtSelected.ClientID%>');
   window.onload = callTest;
</script>
Gonzalo.-
  • 12,512
  • 5
  • 50
  • 82
kein
  • 19
  • 1
1

I use the following code in my .js file, when I have no other better choice.

$("[id$='buttonXXX'])
ann
  • 576
  • 1
  • 10
  • 19
s k
  • 4,342
  • 3
  • 42
  • 61
-1

I know old question but with jquery u can use this approache:

Aspx file

<asp:Button ID="btnCalculate" ClientID="btnCalculate" runat="server" />

external js file

$("[ClientID='btnCalculate']").

https://lanitdev.wordpress.com/2009/06/08/extending-jquery-to-select-asp-controls/

GermanSniper
  • 249
  • 1
  • 5
  • 19