jQuery Trigger .NET Event


Sponsored Links

73058_New Scooba® 230 Floor Washing Robot + Free Shipping!

 

jQuery Trigger .NET Event

Here we look at how to use jQuery to trigger an ASP.NET Event.

Well it's possible to use to jQuery code to trigger ASP.NET events from the client side, such as the Click event of an ASP.NET Button.

You need to know the client ID of the ASP.NET control, so the .NET 4.0 feature of being able to control the client ID comes in extra handy, specifically the 'Static' setting which allows one to control the exact client ID of the ASP.NET control. Ensuring you haven't duplicated the id elsewhere, you can specify unique ids for your controls manually, making finding the control with jQuery alot easier than with the automatically generated ids which ASP.NET creates when using a mode other than 'static'.

Trigger ASP.NET Button Click Event

Once we have the client ID of the ASP.NET control, we can then use jQuery to target that control with a selector and then simply calling .trigger() we can specify the event from ASP.NET which we want, such as in the following example:


$('#MarsButton').trigger('click');


jQuery Trigger .NET Event Example

Here is a full example of how to call an ASP.NET Button's server Click event using jQuery.


<script type="text/javascript">

    $(document).ready(function () {
        $('#HTML_Mars').click(function () {
            $('#MarsButton').trigger('click');
        });
    });

</script>