|
<%@ Page Language="C#" AutoEventWireup="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function GetSynchronousJSONResponse(url, postData) {
var xmlhttp = null;
if (window.XMLHttpRequest)
xmlhttp = new XMLHttpRequest();
else if (window.ActiveXObject) {
if (new ActiveXObject("Microsoft.XMLHTTP"))
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
else
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
url = url + "?rnd=" + Math.random(); // to be ensure non-cached version
xmlhttp.open("POST", url, false);
xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xmlhttp.send(postData);
var responseText = xmlhttp.responseText;
return responseText;
}
function HelloWorld() {
var result = GetSynchronousJSONResponse('<%= Page.ResolveUrl("~/WebService.asmx/HelloWorld") %>', null);
result = eval('(' + result + ')');
alert(result.d);
}
function HelloMe() {
var result = GetSynchronousJSONResponse('<%= Page.ResolveUrl("~/WebService.asmx/HelloMe") %>', '{"Name":"' + document.getElementById('txtName').value + '"}');
result = eval('(' + result + ')');
alert(result.d);
}
function CheckForDuplicateInDB(sender, arg) {
var result = GetSynchronousJSONResponse('<%= Page.ResolveUrl("~/WebService.asmx/CheckName") %>', '{"Name":"' + arg.Value + '"}');
result = eval('(' + result + ')');
arg.IsValid = !result.d;
}
function CallError() {
var result = GetSynchronousJSONResponse('<%= Page.ResolveUrl("~/WebService.asmx/CallError") %>', null);
result = eval('(' + result + ')');
if (typeof (result.d) == 'undefined')
alert(result.Message);
else
alert(result.d);
}
</script>
</head>
<body>
<form runat="server">
<dl>
<dt>Simple Call</dt>
<dt><a href="javascript:HelloWorld()">Hello World</a></dt>
<dt><hr /></dt>
<dt>Call With Argument</dt>
<dt><input id="txtName" value="Your Name" />
<a href="javascript:HelloMe()">Hello Me</a>
</dt>
<dt><hr /></dt>
<dt>Call With Custom Validator</dt>
<dt>
<asp:TextBox ID="txtCheckName" runat="server" Text="Validator"></asp:TextBox>
<asp:CustomValidator ID="cvName" ControlToValidate="txtCheckName" EnableClientScript="true" ClientValidationFunction="CheckForDuplicateInDB" runat="server" Display="Static" ToolTip="Name is Duplicate." Text="*"></asp:CustomValidator>
<asp:Button ID="btnClickMe" runat="server" Text="ClickMe!" />
</dt>
<dt><hr /></dt>
<dt>How to handle server Error</dt>
<dt><a href="javascript:CallError()">Call Error</a></dt>
<dt><hr /></dt>
</dl>
</form>
</body>
</html>
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.