How do i add a Javascript object for a User control in ASP.Net

322 Views Asked by At

I have a User control and i have a method isDirty() added to this user control. This user control is used in many places.

How do i access this method using javascript .How do i create a javascript object for this particular user control and then access it with the find method ?

The $find('__The id of the user control ') ? I tried get null .???

Thanks & Regards, Francis

1

There are 1 best solutions below

2
On

js object is created like this:

var myobject = { prop1: 'hello', 
               prop2: function(){//whatever}, 
              prop3:'yay'};

myobject.prop1; //'hello'
myobject.prop2(); //that's right, it runs that function(){//whatever} in prop2

You cannot access any server side methods with JS directly. Make an AJAX request with a JSON object, parse it in ASP.NET, and in ASP.NET, call your function you want based on what it says in the object.

Flash can use ExternalInterface.addCallback to do a direct JS access but I am not sure ASP.NET has such a thing (and I would strongly recommend against using a "Flash's blackbox JS access" style anyway).

If you already added a JS object to your user control somehow server side by executing javascript, you just access it like this:

myUserControlAssociatedJSObject.myFunction(); //it runs it!