Since there are a lot of people asking how to call a web service in the new Beta version, I am going to post it here:
Testpage.aspx:
<%@ Page Language="C#" EnableViewState="true" AutoEventWireup="true" CodeFile="testpage.aspx.cs"
Inherits="testpage" %>
<!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>Test Page</title>
</head>
<body>
<form id="form1" runat="server" enableviewstate="true">
<atlas:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<atlas:ServiceReference Path="~/WebService.asmx" />
</Services>
</atlas:ScriptManager>
<input type="button" onclick="callWebService();" value="get time" />
<div id="Label">Loading ...</div>
<script type="text/javascript">
function callWebService()
{
WebService.GetServerTime(onComplete);
}
function onComplete(result)
{
$get('Label').innerHTML = result;
}
</script>
</form>
</body>
</html>
WebService.cs:
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
/// <summary>
/// The client code will call this web service to get the server time
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[Microsoft.Web.Script.Services.ScriptService]
// Dont forget to include this scriptservice attribute
public class WebService : System.Web.Services.WebService {
public WebService () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public String GetServerTime()
{
return DateTime.Now.ToString();
}
However if you are calling PageMethods instead of WebService.asmx, you would have to:
"Replaced the instance page method in the CTP release with static page method support."
AND, you have to put your page method at the aspx-page level, eventhough you have code-behind files for the particular page (I think this is a bug). So for example:
<%@ Page Language="C#" EnableViewState="true" AutoEventWireup="true" CodeFile="testpage.aspx.cs"
Inherits="testpage" %>
<!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>Test Page</title>
<script runat="server">
[System.Web.Services.WebMethod]
public static String GetString()
{
return "This text is generated at: " + DateTime.Now.ToString();
}
</script>
</head>
<body>
<form id="form1" runat="server" enableviewstate="true">
<atlas:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<atlas:ServiceReference Path="~/WebService.asmx" />
</Services>
</atlas:ScriptManager>
<input type="button" onclick="callWebServer();" value="call web service" />
<div id="Label">Web Service Call Label</div>
<div id="Label2">Page Methods Label</div>
<script type="text/javascript">
function callWebServer()
{
WebService.GetServerTime(onComplete);
PageMethods.GetString(onCompletePageMethods);
}
function onComplete(result)
{
$get('Label').innerHTML = result;
}
function onCompletePageMethods(result)
{
$get('Label2').innerHTML = result;
}
</script>
</form>
</body>
</html>
The callWebServer function will call both the same webservice from the previous example and the GetString() page method.
That's all. I hope it helps anyone who needs it.
Subscribe to Post Comments [Atom]
October 2006 November 2006 December 2006 June 2007 February 2008
Subscribe to Posts [Atom]