Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
(ebook) Visual Studio .NET Mastering Visual Basic.pdf
Скачиваний:
137
Добавлен:
17.08.2013
Размер:
15.38 Mб
Скачать

SUMMARY 1081

The ItemCommand event is raised when any button or hyperlink on the control is clicked; that’s why the code examines whether the event was fired by a pager item. In this case, the event handler is terminated. If the event was fired by a button, the code executes the DAOrders object’s SelectCommand passing the selected customer’s ID as argument and then fills the DSOrders1 DataSet. The last step is to refresh the DataGrid control with the orders by calling its DataBind method.

With similar statements, the third DataGrid control on the form is populated with the selected order’s detail lines:

Private Sub DataGrid2_ItemCommand(ByVal source As Object, _

ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _ Handles DataGrid2.ItemCommand

Dim orderID As Integer = DataGrid2.DataKeys(e.Item.ItemIndex) DADetails.SelectCommand.Parameters(“@OrderID”).Value = orderID DADetails.Fill(DSDetails1, “Order Details”)

DataGrid3.DataBind() End Sub

For the sake of completeness, I’m including the listing of the control’s PageIndexChanged event handler, which enables the user to navigate to another page of the DSCustomers DataSet by clicking one of the page links at the bottom of the control:

Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, _

ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) _ Handles DataGrid1.PageIndexChanged

DataGrid1.CurrentPageIndex = e.NewPageIndex DACustomers.Fill(DSCustomerNames1, “Customers”) DataGrid1.DataBind()

End Sub

The MasterDetail Web application is quite functional and is typical of the type of application you’ll be called to develop, along with order-processing applications. The code is minimal, and all the functionality required to build the application resides in the control itself. It will take you longer to set the properties of the DataGrid control than to actually write the code.

Summary

This chapter was about ADO.NET on the Web. We focused on data-binding techniques and the visual tools, because this is how you’re supposed to make the most of a rapid application development (RAD) environment like VB. Just about everything we did with point-and-click operations in this chapter can be done with code. As you familiarize yourself with both ASP.NET and ADO.NET, you’ll be able to switch between the visual tools and the code at will.

If you were asked to compare the ease of use of ADO.NET with Windows applications and Web applications, wouldn’t you agree that ADO.NET is better suited for Web applications? ADO.NET was designed for disconnected applications, and the only true disconnected applications are the ones that run on the Web. In Chapter 25, the last chapter of the book, you’ll learn yet another way to use ADO.NET on the Web, through Web services. Web services are the hottest topic in the industry (along with XML, of course).

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

Chapter 25

XML Web Services

The last topic discussed in this book is one of the hottest new features of Visual Studio

.NET, XML Web services, or simply Web services. A Web service is a Class that resides on a Web server and services requests, just like an ASP application. The difference is that the Web service doesn’t furnish HTML pages to the client. Instead, it behaves like a function: clients call the function by name, pass arguments (if needed), and get back a result. The result can be number, a string, an object like a DataSet, or an image. In the examples of this chapter, you’ll see how you can write Web services that return DataSets and how these DataSets can be used to populate DataGrids or other data-bound controls on the client. You can also save the DataSet’s rows to a local file in XML format and use it in other applications. This is actually a very efficient method of making data like price lists available to a large number of recipients.

This is literally a how-to chapter that shows you how to build Web services. No special programming background is required to build Web services; if you can write a function in VB, you can write a Web service. Of course, if your VB function must be executed in the environment of Internet Information Services (IIS), it must encode its result in a form suitable for transmission over the HTTP protocol so that it can respond to requests made over the Web. Theoretically, you can write an ASP application that does the same, but it’s not an easy task. Visual Studio makes building and using Web services as easy as writing and using VB functions in a Windows application. Visual Studio does a lot for you behind the scenes, so that you can think of Web services as regular classes that expose methods—except these methods are available on the Web.

How to Serve the Web

A Web service is a Class that exposes methods, which are in effect functions. To contact the services of a Web service, you must create an object variable that references the specific Web service, just as you can access the members of any class through a variable. Programmers and managers at Microsoft think that Web services will take the Web by storm and that they’ll change the way we program on the Web. It just might happen. You may remember the Web Classes (a tool for abstracting ASP applications that came with VB6). No real developer ever used this tool to build ASP pages; they would much rather build ASP pages with VBScript.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

1084 Chapter 25 XML WEB SERVICES

Web services are not clumsy, like Web Classes were. It’s a nicely implemented feature, and we should see many sites offering their services on the Web in the form of Web services. If you’re wondering what type of services you might offer on the Web, I’ll give a few examples momentarily. But first, I would like to make clear that Web services are nothing more than glorified classes or functions. If we pretend for a moment that we know nothing (or don’t care) about the complexity of the Web or the problems with firewalls and security, we should be asking ourselves: If we can write a VB function and use in multiple Windows applications, why not be able to do the same with the Web? If this question sounds reasonable to you, then Web services are just classes that make their functionality available on the Web.

To better understand the need for Web services, I’ll discuss a couple of examples. Let’s say you’re working for a publisher and you get requests from online stores for information about the books your company publishes. Webmasters need book descriptions, cover photos, anything that will help them better present your books on their site. You can create new documents and send them out on a daily basis, or come up a centralized mechanism for distributing the same information to multiple destinations. As you can guess, different sites may request this information in drastically different formats, from text or Excel files to XML documents.

The centralized mechanism is a business-to-business site that people can connect to and request information from. If they don’t have the cover photo for a specific book, they should be able to connect to this site and make a request like “send me the cover image of the book with ISBN=xxx.” This request should be implemented with a function like GetCoverByISBN(isbnNumber). The GetCoverByISBN() function will return an image, which the other site can either store on its server and reuse or embed directly in its output for a specific client. You can do something similar by opening the HTML page of the book and downloading the file with the cover photo. This means that you must write a bit of code to parse the page, locate the name of the file, and then download it from the server. If the layout of the page changes, you must also change the code that parses the page.

If you could provide a function that could be called over the Web and return a single GIF file, then every online store could use this function. Programmers could even insert the name of the function in the place where the image would normally appear on the page. This way, every time a visitor hits the page, the image will be requested from your server and embedded in their page.

Another example is a function that returns the current price of a product. Any online store that sells your products will offer up-to-date prices and specials without any special arrangements. Or you can post a list of special offers and know that other sites can grab this information and use it immediately.

Other more advanced examples include integrated financial services. A highly secure site could collect information from banks, brokers, and credit organizations and present a unified picture of your finances. We expect to see this type of application in the very near future. Whether the information will flow from Web service to Web service remains to be seen, but this is one of Microsoft’s contributions to an interconnected world of dissimilar computers.

But will other operating systems and Web servers be able to interact with Web services? The secret ingredients that make it all work are XML and SOAP (Simple Object Access Protocol). They’re both open standards and are not very difficult to implement. You can actually write an ASP page that returns one or more values in XML or SOAP format and send it to the client instead of an HTML page. These two formats allow you to send any type of information over the HTTP protocol and through firewalls, so if the other end is capable of handling the XML tag, it will be able to use the Web service. As of now, all major players are promising to support XML, and this makes Web services a very promising technology, because they’re based on standards.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com