How to call function using Jquery on button click event ?


Hi

So many time we will get scenario to call function using jquery on button click event. Even though this is one of frequently asked question in interview.

So here is the complete syntax for doing this task in asp.net master.

Note: Here i have already kept the jquery lib path reference in master page.


<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <script type="text/javascript">
        //   This synstax used for firing some action on click event

        //        $(document).ready(function () {
        //            $("#<%= btnClick.ClientID %>").click(function (event) {
        //                alert("Hello");
        //            });

        //        });


        $(document).ready(function () {
            $("#<%=btnClick.ClientID %>").bind('click', function () {
                Sayhello();
            });

        });

        function Sayhello() {
            alert("Hello from Jquery");
        }

    </script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:Button ID="btnClick" runat="server" Text="Click Here" />
</asp:Content>


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: