views:

892

answers:

4

Hi,

I have a custom control with a bunch of buttons and inputs. I also have a JS file where all my code goes for easy debugging.

How can I get the ClientIDs of the controls in the JS file?

My workaround at the moment is to have a script block at the top of the control and to put all the JS in there. I'd like some separation though.

Many thanks

+1  A: 

Put your controls ID's into page as global variables. Then in your external javascript files use this global variables as reference to your controls.

In your custom control, page or user control :

string script = "<script>var myButtonID = '" + myButton.ClientID + "';</script>";
if (!Page.ClientScript.IsClientScriptBlockRegistered("myGlobalVariables"))
{
    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "myGlobalVariables", script);
}

And in your external JS file :

var myButton = document.getElementById(myButtonID);
//var myButton = $('#' + myButtonID);

EDIT : Here is the way to render your controlIDs in your aspx / ascx files :

<script language="javascript">
     var myButtonID = '<%= muButton.ClientID %>';
</script>
Canavar
Hmmm, I know its only a small amount of code but it might be easier if I just use normal HTML tags that aren't rendered on the server.
Vince
A: 

If the separation is really important to you, moving forward you may want to consider jQuery which has loads of ways to find your controls.

Chad Grant
Most of which are pretty expensive, performance-wise
roosteronacid
I think I'd rather move some of the load to the client, reducing the amount of work on the server. I think jQuery is a good option.
Sir Psycho
A: 

I usually pass an object literal as an argument to my functions. This approach takes away the need of using global variables and makes the disconnect from ASP.NET controls/JavaScript functions a little less complex and more verbose:

JavaScript:

function fn(ids)
{
    var controlOne = ids.controlOne,
        controlTwo = ids.controlTwo,
        controlThree = ids.controlThree;
}

ASP.NET:

fn({
    controlOne: "<%= ControlOne.ClientID %>",
    controlTwo: "<%= ControlTwo.ClientID %>",
    controlThree: "<%= ControlThree.ClientID %>"
});



Another approach would be to target your inputs by class-names:

ASP.NET:

<asp:TextBox ... CssClass="controlone" />
<asp:TextBox ... CssClass="controltwo" />
<asp:TextBox ... CssClass="controlthree" />

JavaScript:

function fn()
{
    var controlOne = document.getElementsByClassName("controlone"),
        controlTwo = document.getElementsByClassName("controltwo"),
        controlThree = document.getElementsByClassName("controlthree");
}



All of this can be simplified greatly by using jQuery or some other JavaScript framework.

roosteronacid
A: 

Or there is another approach that u will define an object is waiting a json object like this

this.Collection  = {'controlName': '<%= controlId.ClientID %>' } ;

Put this at the end of your asp page , the first part will refer to any string to remember your control name and the second part is the real clientID

Inside your javascript file all you need to do call your collection with the control name you assigned

alert(Collection['controlName']) ; // the result will be the clientid for the server side control

hope it work with u cause it's seems simple to me

Amgad Fahmi