Ajax Web Method in Asp.net C#

HTML PAGE
<div>
Your Name :
<asp:TextBoxID=”txtUserName” runat=”server”></asp:TextBox>
<inputid=”btnGetTime”type=”button”value=”Show Current Time”
    onclick=”ShowCurrentTime()”/>
</div>
JAVASCRIPT
<script src=”scripts/jquery-1.3.2.min.js”type=”text/javascript”></script>
<script type=”text/javascript”>
function ShowCurrentTime() {
    $.ajax({
        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) {
            alert(response.d);
        }
    });
}
function OnSuccess(response) {
    alert(response.d);
}
</script>
SERVER SIDE
[System.Web.Services.WebMethod]
publicstaticstring GetCurrentTime(string name)
{
    return”Hello ” + name + Environment.NewLine + “The Current Time is: “
        + DateTime.Now.ToString();
}
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