Raising javascript events in an ASP.NET custom control

Is it possible to raise a "custom event" in an ASP.NET custom control and subscribe to that event on the calling page? This way, I can have any containing page react to changes in the user control without doing a partial or complete postback.

+1


source to share


1 answer


You are trying to mix server side code with client side code. You are probably better off using javascript inline events:

control.Attributes.Add("onclick", "mgr.Click();"); //server-side

//Javascript:
var mgr = new Object();
mgr.Click = new function(e) { //...

      

However, with enough effort and a lot of hacking, you could probably get it to render enough javascript and use AJAX calls to possibly get the effect; but it probably wouldn't be worth it; and is not built in.



Or - if your goal is to raise custom events on the server side (not client side) and handle them in javascript, output the function calls:

this.Controls.Add(new LiteralControl("<script type=\"text/javascript\">mgr.Filter('people');</script>"));

      

+2


source







All Articles