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.
Well, well, well ... After playing with the Beta release today, I am very overwhelmed with the changes that they made from CTP to this Beta 1.0 version.
First in order is of course the changes in web.config file. At first I tried to manually rewrite the web.config file by copy-pasting the code from the beta's web.config. It turns out that this approach caused me tons of problem. The most annoying one is that standard <asp:???> tags didn't get recognized (even I rebuild and rebuild the website). Then, I just erased my web.config and copy the web.config file from the beta and add some lines like <connectionstrings> or <system.net> into my web.config file. After that, I followed the AjaxToolkit guide in modifying web.config and Voila! the page compiled successfully with no errors.
Second in order is modifying my codes. This is where I spend most of the time since there are a lot of codes that I need to rewrite(modify) in order to adapt to the beta release.
<atlasToolkit:ConfirmButtonExtenderID="cbe1"runat="server">
<atlasToolkit:ConfirmButtonProperties TargetControlID="LinkButton1"
ConfirmText="Delete Item?" />
<atlasToolkit:ConfirmButtonProperties TargetControlID="LinkButton2"
ConfirmText="Update Item?" />
</atlasToolkit:ConfirmButtonExtender>
<!-- becomes -->
<ajaxToolkit:ConfirmButtonExtender ID="cbe12" runat="server"
TargetControlID="LinkButton1"
ConfirmText="Delete Item?" />
<ajaxToolkit:ConfirmButtonExtender ID="cbe2" runat="server"
TargetControlID="LinkButton2"
ConfirmText="UpdateItem?" />
Yes, no more properties, just extender. Simplicity works well here.
But, yikes, this is a lot of work!
I love the performance gained by the beta release. My pages are more responsive, and they load very fast (eventhough the site is not precompiled). And surely, MS needs to write better documentation and better migration document. I feel that I get very confused reading them. There are value-add, ajax-core (RTM), clientFX, serverFX, and AJAX CTP itself in this release. Which one control which? Which one support xml-script? Which one don't? Other than that, Intellisense is gone at the content page-level (just for standard asp tags) when using updatepanel in masterpage that wraps the contentplaceholder. One solution to this is to change the "asp" prefix for the Ajax controls back to "atlas". I hope they will fix this soon.
"In the RTM release, the load event is also raised on partial page refresh scenarios, and youHow excited!!!???!!! Well, may be not for you but It really is for me since my project got decent bugs in creating Atlas object/recreating Atlas object with partial page rendering(it really frustates me, k?). So, I am really hoping that this release will solve my problems.
can therefore run code in the pageLoad function to manipulate objects that may have been
recreated as a result of the round trip."
Apparently, MS has a nice program to compose blog offline. This post is just to test the program.
Inserting code:
1: new Option(_firstItemText, "");
Insert map:
failed, it says that blogger does not support this. Too bad :(
Insert hyperlink:
Now, Publishhh!!
This is not related to web technology, but what the heck, this is worth posting. |
"The Service Broker should be enabled by default. Would you please give it a try on it? Please let us know if you have any further enquiries or concerns. Thank you."
October 2006 November 2006 December 2006 June 2007 February 2008
Subscribe to Posts [Atom]