Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 14 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
14
Dung lượng
448,24 KB
Nội dung
Enhancing Web Applications by Using Web Parts Chapter 9 Being a Web developer, you need to develop websites that appeal users. Users today want to do much more than just viewing the content on the website. They want to personalize Web pages according to their interest and preferences. You can implement personalization on your websites by implementing Web parts. Users can use these Web parts to personalize Web pages. In addition to using Web parts, you can enhance a Web application by including commonly-used features such as information about astrology, latest news updates, cricket scores, temperature of the day, or weather forecast. These features are common and are provided on the Internet through Web services. You can easily include these features on your website by consuming existing Web services. This chapter discusses Web parts and their implementation. In addition, it discusses how to consume Web services for providing various features in a Web application. In this chapter, you will learn to: Implement Web parts in a Web application Consume Web services in a Web application Objectives NIIT Enhancing Web Applications by Using Web Parts 9.3 Today, a website may contain a wide variety of information regarding various domains, such as entertainment, astrology, business, sports, and technology. However, users may be interested in viewing only the information of their interest. Therefore, websites such as MSN.com organize their data into discrete units. Each unit represents a standalone part of the website and contains information about a specific domain. These units support a degree of personalization. Users can rearrange or hide these units to suit their individual working styles. These units can be developed by using ASP.NET and are known as Web parts. A Web part is a modular unit that can contain any type of Web-based information. This information could be a picture, static text, or database information. A Web part enables a user to create a personalized user interface and enables you to present information in a better way. It also enables you make your websites interactive. Web parts allow the information displayed on a website to be broken down into manageable parts. Users can rearrange, move, or close the Web parts to suit their preferences. Users can also save their preferences for future browser sessions. The advantages of using Web parts on a website are: Web parts enable users to personalize the content of a Web page. Users can move or hide Web parts to customize the Web page. They can even add new Web parts to change the layout of the page. Web parts can be assigned role-based access, thereby, determining which Web part can be shared by all users or which should be hidden for certain roles. This helps in providing customized content based on security. Web parts can be connected to each other. One of the connected Web parts is defined as a provider and the other as a consumer. Consider the example of a Web parts page that contains two Web parts, one displays a chart and another displays the stock market data. These Web parts can be connected in such a way that the chart Web part displays the chart for the stock market data present in the other Web part. In this example, the Web part providing the stock market data is the provider and the Web part displaying the chart is the consumer. A dvantages of Using Web Parts Implementing Web Parts in a Web Application 9.4 Enhancing Web Applications by Using Web Parts NIIT N ot e Personalization is one of the main advantages of using Web parts. You can implement personalization by allowing a user to view a Web page in several display modes. Display modes allow users to modify or personalize the Web page. Users can edit the Web part controls, change the layout of the Web page, and add new Web part controls by using different types of display modes. The different types of display modes available are: Browse: This is the default mode. This mode allows users to view Web parts on the Web page. It also allows users to minimize or close the Web part. If you close a Web part, it cannot be restored from the Browse mode. However, you can restore it in another mode called the Catalog mode. The following figure shows a Web part in the Browse mode. A Web Part in the Browse Mode Edit: This mode allows users to edit Web parts on the Web page at runtime. Users can set the title, color, or custom properties of the Web parts. Design: This mode allows users to rearrange the order of Web parts on the Web page by dragging and dropping them. Catalog: This mode allows users to add new Web parts on the Web page. It also allows users to rearrange or delete the existing Web parts from the Web page. Connect: This mode allows users to connect two Web part controls on a Web page. A Web page can be displayed in only one display mode at a time. ASP.NET provides you with the Web parts control set that are designed to implement Web parts on your websites. A Web parts control set consists of a group of Web parts that work together to enable users to create Web parts pages. A Web page that is composed of Web parts is known as a Web parts page. A Web parts page is divided into zones that provide the structure for placing Web parts. You can place Web parts in any of these zones according to your requirements. W eb Part Modes W eb Parts Control Set NIIT Enhancing Web Applications by Using Web Parts 9.5 The following table describes some of the Web part controls available in ASP.NET. Control Description WebPartManager Manages all the Web part controls on a page. Each Web parts page contains exactly one WebPartManager control. CatalogZone Contains CatalogPart controls and is used to create a catalog of Web part controls from which a user can select a Web part control to add to a page. EditorZone Contains EditorPart controls and is used to enable a user to edit and personalize Web part controls on a page. WebPartZone Contains the overall layout for the Web Part controls that compose the main user interface of a page. ConnectionsZone Contains Connection controls. CatalogPart Contains a list of available Web part controls that a user can add to a page. Connection Creates a connection between two Web part controls on a page. EditorPart Serves as the base class for specialized controls that provide the functionality to edit Web parts. Some of the Web Part Controls Available in ASP.NET To implement Web parts on your website, you first need to add a WebPartManager control and one or more WebPartZone controls to the Web page. The WebPartManager control is used to manage all the Web part controls on the Web page. The WebPartZone control serves as a container for the Web part controls. After adding these controls, you need to add the controls on your Web pages. These controls can be the existing Web server controls or Web user controls. Once you have created a Web parts page, you can allow users to change the appearance of the user interface elements on the Web page. This can be done by changing the display mode of the Web parts page. To enable a user to switch between the various modes, you can include a drop-down list on the Web page that allows a user to select the desired mode. After including the drop-down list on the Web page, you can type the following code snippet in the SelectedIndexChanged event of the DropDownList control: WebPartManager wpm = WebPartManager.GetCurrentWebPartManager(Page); string selectedMode = DropDownList1.SelectedValue; 9.6 Enhancing Web Applications by Using Web Parts NIIT WebPartDisplayMode mode = wpm.SupportedDisplayModes[selectedMode]; if (mode != null) { wpm.DisplayMode = mode; } The preceding code snippet, enables a user to change the mode of the Web parts present on the Web page by selecting the edit mode from the DropDownList control. Activity 9.1: Creating a Web Parts Page There are situations where you need to connect sets of data from different data sources. For example, a Web parts page contains two Web parts, one that contains details of senior managers of a department and the other that displays their photographs, one at a time. You can connect these Web parts in such a way that when you select the name of a manager in the first Web part, the other Web part displays the photograph of the selected manager. In the preceding example, the Web part containing the details of senior managers is the provider and the Web part that displays the photograph of the selected senior manager is the consumer. Web part connections are based on a pull model, where the consumer pulls data from the provider. To create a connection, the provider control defines a communication contract in the form of an interface, indicating the data it can provide. The consumer control knows about this contract and retrieves the data through the connection. The interface can contain properties, events, or methods that the consumer control can use once the connection is established. To participate in a connection, a Web part needs to define a connection point. A connection point contains the details necessary for a server control to connect to another control. These details include information such as the type of the control itself, the type of data the control recognizes, an ID for the connection point object, and whether the control can form multiple connections. Connection points are of two types, provider connection points and consumer connection points. To create connected Web parts, you need to perform the following tasks: 1. Create the provider Web part as a custom control that derives from the WebPart class and implements an interface that defines the communication contract. The provider Connecting Web Parts NIIT Enhancing Web Applications by Using Web Parts 9.7 Web part defines a connection point in the form of a method to which the ConnectionProvider attribute has been applied. 2. Create the consumer Web part as a custom control that derives from the WebPart class and defines a connection point in the form of a method to which the ConnectionConsumer attribute has been applied. 3. Add the Web parts to the Toolbox. 4. Add the Web part controls from the Toolbox to a Web parts page and assign an ID and title to each of the two controls. 5. Add the connection information to the WebPartManager control by opening the Web parts page in the Source view and adding a <staticConnections> element inside the <asp:WebPartManager> element, as shown in the following example: <StaticConnections> <asp:WebPartConnection ID="wpCon" ProviderID="Provider1" ProviderConnectionPointID="Color" ConsumerID="Consumer1" ConsumerConnectionPointID="Color" /> </StaticConnections> Task 9.1: Connecting Web Parts 9.8 Enhancing Web Applications by Using Web Parts NIIT N ot e N ot e There are certain things that most websites require. For example, almost all websites provide the searching functionality on their home pages. One way to provide this functionality on your website is to write your own code. The other way is to use an existing code, and this can be done by using Web services. Web services are the application components that can be used by other Web applications. You can create a Web service and publish it on a Web server, thereby, making it available for other Web applications to use. A Web service is a self-describing Web component that exposes its functionality to the consumers through open standards such as XML and Simple Object Access Protocol (SOAP). It is a widely used method for implementing Service-Oriented Architecture (SOA). SOA is software architecture, which is essentially a collection of services communicating with each other. It was developed for enabling the development of loosely-coupled distributed applications. Coupling refers to the interrelatedness and interdependency between two or more components of a Web application. In loosely-coupled distributed applications, it is possible to change one component without affecting other components. Web services allow integration of applications developed in different languages and running on different platforms. This integration and interoperability in Web services is made possible with the use of XML. Web services communicate by using a standard protocol called SOAP. SOAP defines a standard way of passing the XML-encoded data. Developing ASP.NET Web services starts by creating an .asmx file and hosting it on a Web server, such as IIS. Once hosted or published on the server, a Web service can be consumed by any client. The client can be a Web application, desktop application, console application, or another Web service based on .NET or other platforms such as Java. Invoking Web Services Consuming Web Services in a Web Application NIIT Enhancing Web Applications by Using Web Parts 9.9 The client, who has to consume a Web service, locates the service by adding a Web reference. This adds a proxy class on the client side, which exposes the methods, parameters, and return types of the methods contained in the Web service. After adding the proxy class, the client application creates an instance of the proxy class and accesses the methods provided by the Web service through the instance. The client requests are then handled by the proxy class. It transforms the method calls of the client into the SOAP message format, which can be transmitted over HTTP to the server. After processing the request, the response is also sent as a SOAP message to the proxy. The proxy then converts this SOAP message into method return value, which is returned to the client. Activity 9.2: Creating a Web Part That Uses a Web Service In distributed computing, Web services along with various other technologies, such as COM+, .NET Remoting, Message Queues, and Web Service Enhancement (WSE) were used to provide a wide range of functionality. Every technology is a discreet set of services fulfilling a specific requirement of a distributed development. This restricts the developers to make a choice of one of these technologies. If a developer decides to use more than one technology, a lot of code needs to be written and unification becomes tedious. In addition, developers need to learn different technologies to cater to the different needs of distributed application development. For example, to provide security to the Web services, WSE support needs to be separately added with the Web service. Similarly, if there is a requirement to host a Web service on TCP and HTTP, separate coding needs to be defined for both. To overcome the complexity in the integration of applications developed by using different technologies, Windows Communication Foundation (WCF) was introduced. WCF unifies features of such discreet technologies under one single framework. It provides a unified programming model used to build a secure, reliable, and robust Web service. WCF aims at providing encoding, hosting, messaging patterns, networking, security, and interoperability in a single infrastructure. This helps in simplifying the development of distributed applications. Invoking WCF Services 9.10 Enhancing Web Applications by Using Web Parts NIIT N ot e The following figure shows the WCF architecture. The WCF Architecture The following set of APIs is unified in the WCF architecture: ASP.NET Web Services (ASMX): It provides support and interoperability with Web services running on non-Windows platforms. WSE: It provides end-to-end standards-based security for Web services. System.Messaging: It is a namespace in the .NET Framework that provides managed APIs to Microsoft Message Queues (MSMQ), enabling developers to build asynchronous reliable distributed applications. Enterprise Services: It provides managed APIs to COM+. COM+ provides component-based programming for functionality such as transactions. Remoting: It exposes the Common Language Runtime (CLR) type system remotely and provides location transparency for objects. Remoting is designed for tightly coupled .NET-to NET application communication. It provides seamless and simple development experience for applications on the local network and the Internet. In tightly coupled applications, the components are interconnected with each other such that any change in one component will lead to changes in other components as well. [...]... of a Web page Web parts can be assigned role-based access, thereby determining which Web parts can be shared by all users or which should be hidden for certain roles Web parts can be connected to each other The different types of display modes of a Web part are: Browse mode Edit mode Design mode Catalog mode Connect mode A Web page that is composed of Web parts is known as a Web parts page A Web parts. .. following Web part controls contains a list of available Web part controls that a user can add to the page? a EditorPart b Connection c WebPartManager d CatalogPart 9.12 Enhancing Web Applications by Using Web Parts NIIT Summary In this chapter, you learned that: A Web part is a modular unit that can contain any type of Web- based information The advantages of using Web parts on a website are: Web parts. .. Creating a Web Part That Uses a WCF Service NIIT Enhancing Web Applications by Using Web Parts 9.11 Practice Questions 1 Which of the following Web part modes allows users to rearrange the order of Web parts on the Web page by dragging and dropping them? a Browse mode b Design mode c Catalog mode d Edit mode 2 Which of the following Web part controls contains the overall layout for the Web Part controls... interface of a page? a WebPartZone b CatalogZone c WebPartManager d CatalogPart 3 Which of the following APIs, unified in the WCF architecture, provides managed APIs to COM+? a ASP.NET Web Services b WSE c Enterprise Services d Remoting 4 Which of the following Web part modes allows users to add new Web parts on a Web page, rearrange them, and delete existing Web parts from the Web page? a Connect mode... placing Web parts A Web service is a self-describing Web component that exposes its functionality to the consumers through open standards such as XML and Simple Object Access Protocol (SOAP) Windows Communication Foundation (WCF) aims at providing encoding, hosting, messaging patterns, networking, security, and interoperability in a single infrastructure NIIT Enhancing Web Applications by Using Web Parts. .. providing encoding, hosting, messaging patterns, networking, security, and interoperability in a single infrastructure NIIT Enhancing Web Applications by Using Web Parts 9.13 9.14 Enhancing Web Applications by Using Web Parts NIIT ... design goals for building WCF services are: Unification: WCF increases productivity by bringing together various technologies such as COM+ and NET Enterprise services, Message Queues, NET Remoting, ASP.NET Web Services, and WSE for building distributed applications by using a unified programming model Interoperability: WCF increases the interoperability options with NET-to-.NET applications by providing . using Web parts on a website are: Web parts enable users to personalize the content of a Web page. Users can move or hide Web parts to customize the Web page. They can even add new Web parts. the Web parts control set that are designed to implement Web parts on your websites. A Web parts control set consists of a group of Web parts that work together to enable users to create Web parts. pages. A Web page that is composed of Web parts is known as a Web parts page. A Web parts page is divided into zones that provide the structure for placing Web parts. You can place Web parts in