We
thought to expand the idea of Ajax request little bit further in order to
present a very useful concept for achieving a flexible development approach with
in your application. So, CMSN Software presents this tutorial "How to
call CSharp function in Ajax
" which explains how to call CSharp method using JavaScript
via Ajax request.
We
think that you have obtained a clear idea about how to load text file content using
JavaScript via Ajax request by reading
our
previous tutorial "Simple
Ajax Request".
In this tutorial
we
used a simple web service to return sever time.
We
can access that web service using several technologies. In here
we
are going to explain how to use
JavaScript
ASP.NET Ajax
jQuery
to access web service method and display sever time using Ajax request.
This sample shows you how to access CSharp method using JavaScript
through Ajax request.
<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="Default.aspx.cs"Inherits="JavaScriptSample._Default"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title>How to call CSharp function in Ajax | JavaScript Sample</title><scripttype="text/javascript">function getServerTime() {
/// <summary>/// Call ServerTime web method via ajax request/// </summary>if (window.XMLHttpRequest) {
// for IE7+, Firefox, Chrome, Opera, Safarithis.xmlhttp = new XMLHttpRequest();
}
else {
// for IE6, IE5try {
this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e1) {
try {
// older version of Msxmlthis.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
this.xmlhttp = null;
}
}
}
xmlhttp.onreadystatechange = function() {
/// <summary>/// Display server time when success/// </summary>if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
// success Status
document.getElementById("ServerTimeResponse").innerHTML = xmlhttp.responseText;
}
}
this.xmlhttp.open("POST", "AjaxServer.asmx/ServerTime", true);
this.xmlhttp.send();
}
</script></head><body><formid="ServerTimeForm"runat="server"><div><inputid="ServerTimeButton"type="button"onclick="getServerTime();"value="Get Server Time"/><divid="ServerTimeResponse"runat="server"></div></div></form></body></html>
In
our
previous tutorial "Simple
Ajax Request"JavaScript section
We
explained the basic steps of creating a Ajax request using JavaScript.
So
we
are not going to repeat same steps here. The main difference here is
we
are using post method instead of using get method in open(method, url, async)
method of the XMLHttpRequest object. And
we
replace the text file's url by url of the web service.
This sample shows you how to access CSharp method using Asp.Net through
Ajax request.
<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="Default.aspx.cs"Inherits="MicrosoftAjaxSample._Default"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title>How to call CSharp function in Ajax | Microsoft Ajax Sample</title><scripttype="text/javascript">function getServerTime() {
/// <summary>/// Call ServerTime web method via ajax request/// </summary>
MicrosoftAjaxSample.AjaxServer.ServerTime(OnComplete, OnError);
}
function OnComplete(result) {
/// <summary>/// Display server time when success/// </summary>
document.getElementById("ServerTimeResponse").innerHTML = result;
}
function OnError(result) {
/// <summary>/// show error message when there is an error/// </summary>
alert("Error: " + result.get_message());
}
</script></head><body><formid="ServerTimeForm"runat="server"><asp:ScriptManagerID="ServerTimeScriptManager"runat="server"><Services><asp:ServiceReferencePath="~/AjaxServer.asmx"/></Services></asp:ScriptManager><div><inputonclick="getServerTime();"id="ServerTimeButton"type="button"value="Get Server Time"/><divid="ServerTimeResponse"runat="server"></div></div></form></body></html>
In
our
previous tutorial "Simple
Ajax Request"Asp.Net section
we
explained that how to access server method using Script Manager. Because of that
in this tutorial
we
are trying to explain how to call CSharp method using Asp.Net Web
Service.
The major part of this sample is adding Service Reference to Asp.Net Script
Manager. Then Asp.Net Script Manager will take care of rest of the coding.
we can do what ever we want in OnComplete function.
This sample shows you how to access CSharp method using jQuery through
Ajax request.
<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="Default.aspx.cs"Inherits="JQuerySample._Default"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title>How to call CSharp function in Ajax | jQuery Sample</title><scripttype="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script><scripttype="text/javascript">
$(document).ready(function() {
/// <summary>/// A function to execute after the DOM is ready./// </summary>
$("#ServerTimeButton").click(function() {
/// <summary>/// A function to execute when the ServerTimeButton is clicked./// </summary>
$.ajax({
/// <summary>/// Perform an asynchronous HTTP (Ajax) request/// </summary>
type: "POST",
url: "AjaxServer.asmx/ServerTime",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
/// <summary>/// Display server time when success/// </summary>
$('#ServerTimeResponse').html(response.d);
},
failure: function(msg) {
/// <summary>/// show error message when there is an error/// </summary>
$('#ServerTimeResponse').html(msg.d);
}
});
returnfalse;
});
});
</script></head><body><formid="ServerTimeForm"runat="server"><div><asp:ButtonID="ServerTimeButton"runat="server"Text="Get Server Time"/><divid="ServerTimeResponse"runat="server"></div></div></form></body></html>
This sample also almost same as
our
previous tutorial "Simple
Ajax Request"jQuery section. The major difference here
is we are using Ajax request type as "POST" and url as url to our
web service. In the success function we can access the response return from server.
We
have used following sample web service file to get the server time via Ajax
request.
2 comments:
Post a Comment