Tuesday, August 11, 2009

Code: Use jQuery to Bind an Event Handler to a DropDownList

<script type="text/javascript">
function EnableControls() {
}
$(document).ready(function() {
$("select#<%=ddDateType.ClientID %>").change(EnableControls);
});
</script>


If you haven’t yet spent time to learn what jQuery can do for your site, then I highly recommend you do.  Its benefits may seem trivial at first glance, but once you start digging in, its flexibility is very useful.  When the page loads (client side load, not to be confused with the Page_Load server event), jQuery will tell the DropDownList "ddDateType" that it should call the javascript method "EnableControls" when the selected value changes.  This comes in handy if you need to show/hide other controls (there’s also a jQuery method for that) or do some AJAX work (there’s also a jQuery method for that).