O'Reilly logo

Programming ASP.NET 3.5, 4th Edition by Jesse Liberty, Dan Hurwitz, Dan Maharry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Calling the Web Service

You’ve already seen that ASP.NET will create a page to view in your browser if you navigate to a web service directly, but nine times out of 10 this won’t be how a web service is accessed. Instead, a user will browse to a page that calls the web service behind the scenes for information. In this section, you’ll create a simple web page that consumes the StockTickerSimple web service you just created. As you’ll see, VS2008 does all the heavy lifting for you.

With C16_WebServices still open in VS2008, click File → Add → New Web Site and create a new website called C16_WebServiceClients. Click OK, and you’ll see it appear in the Solution Explorer above C16_WebServices, as shown in Figure 16-6.

Two projects in the Solution Explorer

Figure 16-6. Two projects in the Solution Explorer

Now delete Default.aspx and add a new web form called StockTickerClient.aspx. This page will consume your web service. To demonstrate, add a Label control called lblMessage and two Button controls, btnPost and btnWebService, to the page, as highlighted in Example 16-5. The Click event handler for btnWebService will call the web service and display the stock price for Microsoft in lblMessage. Clicking btnPost will cause the page to post back and the Page_Load handler will show the current time in lblMessage.

Example 16-5. StockTickerClient.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="StockTickerClient.aspx.cs" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required