views:

17

answers:

2

I'm building an ASP.net custom control that implements IScriptControl. I would like other users of my control to be able to assign client-side event handlers to the control. Unfortunately the JS generated by IScriptControl is always injected at the very bottom of the rendered page (see below), so any attempt to assign an event handler in the ASPX page fails because the code executes too early.

...
<script type="text/javascript"> 
//<![CDATA[
Sys.Application.initialize();
Sys.Application.add_init(function() {
    $create(MyNamespace.MyControl, {}, null, null, $get("my_control_id"));
});
//]]>
</script>
</form>

What's the right way to assign an event handler to the instantiated control upon page load?

A: 

Actually, they would be able to add the event handler using any of the javascript library's like jQuery using:

$(document).ready(function(){

 $('#<%= my_control_id.ClientId%>').click(function(e){
  // do something...
 });

});

HTH.

Sunny
This is a complex little custom control, and the event to be handled does not correspond to an event on any particular DOM element. The handler must be applied to the object created in the IScriptControl-generated code. Unfortunately the object after any other script blocks have executed.
nw
+1  A: 

Check This

Sys.Application.add_load(handler);
Kronass
That did it! w00t.
nw