Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 701
Ricky Casavecchia Avatar asked May 19 '26 17:05

Ricky Casavecchia


2 Answers

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)

like image 93
Quinn Wilson Avatar answered May 22 '26 07:05

Quinn Wilson


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

like image 26
Ricky Casavecchia Avatar answered May 22 '26 05:05

Ricky Casavecchia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!