3

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.

Ricky Casavecchia
  • 560
  • 3
  • 9
  • 28

3 Answers3

3

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)

Quinn Wilson
  • 7,995
  • 1
  • 22
  • 31
  • yes u r right but sometimes I had to go the (CLIENTID_javascriptFunction) instead of passing a large number of parameters to a global declared function – AHMED EL-HAROUNY Jul 03 '12 at 18:01
1

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

Ricky Casavecchia
  • 560
  • 3
  • 9
  • 28
  • If you did want to use a user control, this seem like a good way to protect the scope -> http://programmerramblings.blogspot.com/2011/07/clientside-api-for-aspnet-user-controls.html – Ricky Casavecchia Jul 12 '12 at 17:22
-1

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

AHMED EL-HAROUNY
  • 836
  • 6
  • 17
  • This is basically what I have been doing. I'm hoping that the guys at Microsoft made a way to allow you to reference the user controls individuality, rather than calling it like this. – Ricky Casavecchia Jul 03 '12 at 20:28
  • It will work, but its really ugly solution. Much better way to resolve that problem is mentioned here: http://stackoverflow.com/questions/4416705/javascript-functions-inside-asp-net-user-control – Jakub Szułakiewicz Nov 29 '16 at 08:37
  • @Jakub Szułakiewicz thanks for that. By the way if you really need a better solution, you should stop using web forms :) this answer is 4 years old – AHMED EL-HAROUNY Nov 29 '16 at 20:36