Ricky Casavecchia
Ricky Casavecchia

Reputation: 640

ASP.NET Scope of user control for javascript functions

So I have a user control that exists multiple times on a page. From the back end I can call userControl1.someFunction(); and specify which user control I want to call someFunction() for. But if I have a java-script function on the front-end of the user control I can't call it for individual user controls. All I have to do is call javaFunction(), but this doesn't specify which user control I want to call. So this is what I would like to be able to do, clientsideUserControl1.javaFunction(); Is this possible to do? What I have been doing is generating the function name dynamically IE: clientsideUserControl1_javaFunction(), but I feel like there has to be a better way to do this.

Upvotes: 3

Views: 1384

Answers (3)

Ricky Casavecchia
Ricky Casavecchia

Reputation: 640

Turns out that in this case it would be better to use a server control instead of a user control. Server controls seem to be a little more complicated to make but they do protect the scope of the javascript functions.

Here is a link that discusses the differences. http://www.hotscripts.com/forums/asp-net/31174-difference-between-user-control-custom-server-controls.html

Upvotes: 1

Quinn Wilson
Quinn Wilson

Reputation: 8233

usually you can have one function and have it perform it's work on the whole page or you can change it to take a parameter ( a reference to the usercontrol you're interested in )

That way you don't need to have multiple copies of the same javascript function.

So instead of

function CLIENTID_javascriptFunction{
}

You'd have on function at the global level :

function javascriptFunction(id){

}

and call it with the id of the dom object you're interested in. (use ClientID to get the DOM id of the control)

Upvotes: 3

AHMED EL-HAROUNY
AHMED EL-HAROUNY

Reputation: 836

one possible solution is this:

function <%= ClientID %>javaFunction()
{
  //code here
}

you will have a function declaration for each user control with the client ID of the control plus function name

Upvotes: -1

Related Questions