Call ASP.Net Page Method using jQuery AJAX Example

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">

    public static string GetCurrentTime(string name)
        return "Hello " + name + Environment.NewLine + "The Current Time is: "
            + DateTime.Now.ToString();


<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <script src="Scripts/jquery-2.1.3.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            function ShowCurrentTime() {
                    type: "POST",
                    url: "Default.aspx/GetCurrentTime",
                    data: '{name: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',
                     contentType: "application/json; charset=utf-8",
                     dataType: "json",
                     success: OnSuccess,
                     failure: function (response) {
             function OnSuccess(response) {
                 $("#DivShowAfterClick").append("<p>" + response.d + "</p>");

            Your Name :
            <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
            <input id="btnGetTime" type="button" value="Show Current Time"
                onclick="ShowCurrentTime()" />

        <div id="DivShowAfterClick" runat="server">This should be invisible until button clicked.</div>



Leave a Reply

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

You are commenting using your 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