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" ...

Get Programming ASP.NET 3.5, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.