|
Write a comment |
Articles |
DHTML Gallery |
.NET |
MyBlog |
About Me |
FAQ
|
|
|
Using Client-Side Custom Events in ASP.NET Applications
Download Code These days many average developers like me find themselves breaking their heads to implement 'desktop like' behaviors for web applications. Many ASP.NET controls (like Button and ModalPopupExtender) have public properties for attaching javascript handlers to client side events. Now a common problem is,
"you want to notify multiple controls about an client side event."
For example:
In more complex scenarios, it might be required to be able to dynamically add and remove handlers, in addition to things mentioned above. I have seen developers do lot of ugly 'wiring' of javascript functions to achieve this. Of course it works , but it is a nightmare to maintain. In this article we will see how to make use of 'client-side custom events to simplify coding for such scenarios.
What are 'Client-Side Custom events' ?
Sample Scenario
![]() Screenshot of the sample application. Doesn't look like a big deal ! So why use custom events, when it is lot easier to write a single function to do all this, and hook it up to 'OnOkScript' property of the ModalPopupExtender. We could do that , but what if , we had 20 such operation ? wont a single function be too long and complex ? And what if we wanted the ability to add/remove handlers dynamically? In the above screen , I have placed checkboxes to facilitate dynamic attaching /detaching of handlers.
Using Custom Events
<ajaxToolkit:ModalPopupExtender PopupControlID="Panel1" ID="ModalPopupExtender1" runat="server"
TargetControlID="Button1" CancelControlID="btnCancel" OkControlID="btnOK"
BackgroundCssClass="modalBackground" OnOkScript="GetNumber();" >
function GetNumber()
{
var i = $("#txtVal")[0].value;
var val = parseInt(i);
if( val != i)
{alert("Please enter a valid integer.");$("#txtVal")[0].value=0;}
else
{ jQuery(document).trigger('okbuttonpressed', val);}
}
Here , 'txtVal' is id of a texbox inside the modal popup where user enters a value. As a best practice, I should have tried to fetch 'ClientId' (since its a severside control) and use it, but since we are not dealing with repeaters and usercontrols , I am using the id directly (also because I am bit lazy). So when 'OK' button is clicked , the above function gets called and the 'okbuttonpressed' custom event gets triggered.
Attaching handlers to custom events
hkAdd.Attributes.Add("onclick", "AddChkChanged(this)");
chkMul.Attributes.Add("onclick", "MulChkChanged(this)");
chkDivide.Attributes.Add("onclick", "DivChkChanged(this)");
function DivChkChanged(x)
{
if(x.checked)
jQuery(document).bind('okbuttonpressed', DivVals);
else
jQuery(document).unbind('okbuttonpressed', DivVals);
}
function MulChkChanged(x)
{
if(x.checked)
jQuery(document).bind('okbuttonpressed', MulVals);
else
jQuery(document).unbind('okbuttonpressed', MulVals);
}
function AddChkChanged(x)
{
if(x.checked)
jQuery(document).bind('okbuttonpressed', AddVals);
else
jQuery(document).unbind('okbuttonpressed', AddVals);
}
Thats all ! Now if you run the page, you can see for yourself how the custom events work. Be sure to check any of the checkboxes, before brining up the modal popup to see calculated result. Thats all from me. Though this was a very simple example, the attempt was to illustrate how using 'custom events' can help in clean coding. Without them our javascript would have been inelegant and full of 'if' statements and global variables. Custom events are much more useful when dealing with more complex scenarios. They pave the way for better event driven programming in javascript. |
| Copyright (c) 2007-2009 Ashish Patil . Please read FAQ for more details. |