Also, in the web application format, the pagename. NET versions before version 2. NET's code-behind model marks a departure from Classic ASP in that it encourages developers to build applications with separation of presentation and content in mind. In theory, this would allow a Web designer, for example, to focus on the design markup with less potential for disturbing the programming code that drives it.
This is similar to the separation of the controller from the view in model—view—controller MVC frameworks. A directive is a special instruction on how ASP. NET should process the page. NET page parser and compiler. User controls are encapsulations of sections of page sections that are registered and used as controls in ASP. Programmers can also build custom controls for ASP. NET applications. Unlike user controls, these controls do not have an ASCX markup file, having all their code compiled into a dynamic link library DLL file.
Such custom controls can be used across multiple Web applications and Visual Studio projects. NET uses a "visited composites" rendering technique. During compilation, the template. Literal text goes into instances of the Literal control class, and server controls are represented by instances of a specific control class.
The initialization code is combined with user-written code usually by the assembly of multiple partial classes and results in a class specific for the page. The page doubles as the root of the control tree. Actual requests for the page are processed through a number of steps. First, during the initialization steps, an instance of the page class is created and the initialization code is executed. This produces the initial control tree, which is now typically manipulated by the methods of the page in the following steps. Finally, during the rendering step a visitor is used to visit every node in the tree, asking each node to render itself using the methods of the visitor.
The resulting HTML output is sent to the client. After the request has been processed, the instance of the page class is discarded and with it the entire control tree. This is a source of confusion among novice ASP. As such, if an application uses stateful interaction, it has to implement state management on its own. NET provides various functions for state management.
Conceptually, Microsoft treats "state" as GUI state. Problems may arise if an application must track "data state"; for example, a finite-state machine that may be in a transient state between requests lazy evaluation or takes a long time to initialize. State management in ASP. NET pages with authentication can make Web scraping difficult or impossible.
Application state is held by a collection of shared user-defined variables. Application state variables are accessed using the Applications collection, which provides a wrapper for the application state. Application state variables are identified by name. Server-side session state is held by a collection of user-defined session variables that are persistent during a user session. These variables, accessed using the Session collection, are unique to each session instance.
The variables can be set to be automatically destroyed after a defined time of inactivity even if the session does not end. Client-side user session is maintained by either a cookie or by encoding the session ID in the URL itself. NET supports three modes of persistence for server-side session variables: . NET pages in a Web application. HTTP is a stateless protocol. The server retains no knowledge of variable values that were used during previous requests. NET session state identifies requests from the same browser during a limited time window as a session, and provides a way to persist variable values for the duration of that session.
By default, ASP. NET applications to maintain the state of the Web form controls and widgets. The server sends back the variable so that, when the page is re-rendered, the controls render at their last state. At the server side, the application may change the viewstate, if the processing requires a change of state of any control. The states of individual controls are decoded at the server, and are available for use in ASP.
NET pages using the ViewState collection. The main use for this is to preserve form information across postbacks. View state is turned on by default and normally serializes the data in every control on the page regardless of whether it is actually used during a postback. This behavior can and should be modified, however, as View state can be disabled on a per-control, per-page, or server-wide basis. Developers need to be wary of storing sensitive or private information in the View state of a page or control, as the base64 string containing the view state data can easily be de-serialized.
Encryption can be enabled on a server-wide and server-specific basis, allowing for a certain level of security to be maintained. NET offers a "Cache" object that is shared across the application and can also be used to store various objects. The "Cache" object holds the data only for a specified amount of time.
Other means of state management that are supported by ASP. User controls and custom controls can be used as Web Parts, although they will lack all of the features of a full-fledged Web Part. But, if you want to take full advantage of the Web Part feature set, then you must build your control as a Web Part right from the beginning. Web Parts actually first appeared not in ASP. With ASP. The usual division of labor is to have developers build Web pages and users.
SharePoint, however, was designed to empower users, to let users build the pages they needed without having to call on the IT staff. In ASP. NET, Web Parts can be used to fulfill the same function: to let users build the pages they need from the inventory of Web Parts available to a page. Because of this ability, Web Part developers have a new and more interesting job to do.
NET best practices guide here. One enables you to map profile properties directly to a SQL table in a database -- enabling you to perform richer queries on the back-end for your personalization data. Patrick pointed me at the cool and free! In addition, the different functional areas of a page often need to be handled as a group of controls for managing layout, for example. HTTP is a stateless protocol.
Once a Web Part is available to a site, developers then add the Web Part to a page. The Benefits of Reusable Controls By creating your own controls, you can build a toolkit of controls to draw on when building a Web application. Think of these controls as reusable visual components. A control can range from something as simple as displaying a title to being a complete business application in itself. Much of the talk about the benefits of creating objects and components seems to revolve around abstract features encapsulation, polymorphism, and so on.
Web Parts, custom controls, and user controls provide all three of these benefits. Web Parts add features that custom controls and user controls do not. The benefits of reusability with object-oriented development are so well known that they form part of the conventional wisdom of modern application developers.
But Web Parts also provide another benefit: customization. The benefits of customization are not as commonly known, however, so the next section describes why customization matters to you. Creating Your Own ControlsBeyond Reusability with Web Parts Through customization, Web Parts give you the opportunity to gain a new and more challenging class of benefits: you can create Web Parts that end users can add to their pages in order to create their own solutions. Think of Web Parts as reusable visual tools rather than just visual components : Web Parts are tools that users employ to meet their goals.
When you create a user control or a custom control you design it to help you and other developers solve problems in creating applications. With Web Parts you create controls designed to let end users solve problems, often in situations that you may not have even thought of. In addition to adding your Web Part to a page, users can also modify the way that your Web Part behaves. Web Parts are about what you can allow your users to do for themselves — how you can empower your users. You can give users the ability to add Web Parts to pages, remove Web Parts from pages, move Web Parts from one location to another on the page, customize the Web Parts on a page, and join Web Parts together so that they can pass infor- mation between themselves.
So, in addition to building applications, you can provide the tools that allow users to build their own solutions. SharePoint, where Web Parts first appeared, was designed to empower users to build solutions that met their needs. NET developer also have the ability to empower your users. Instead of just designing an application to perform some task, you can consider the entire range of activities that your users need to perform and build tools that support those activities in any combination. Instead of delivering a rigid system that implements your design, you can build a discrete set of tools that allows users to meet their own needs.
The obvious solution is to go out and buy a piggy bank. So instead of buying a piggy bank, you could buy a Lego kit.
With a Lego kit you can build your own piggy bank — perhaps even figure out how to build a bank that works well with all the different things that you want to save. You can also build a tower, a plane, a car, and anything else that you can think of. In addition, different Lego kits have different building blocks.
The greater the variety of Lego building blocks available to you, the greater the variety of things that you can build and the easier it is to build specific items a car is considerably easier to build if your kit includes wheels and axles, for instance. Within any application domain, domain-specific tools are more useful than general-purpose tools.
With Web Parts, your job is to provide your user with a set of building blocks that your users can build solutions with. Undoubtedly, the user community for your application will contain a variety of users, many with special- ized needs. Instead of building a single application that must meet the diverse needs of all of your users, you build the Web Parts that your users need, and let your users each build a set of unique applications that meet their needs. Instead of building a single application, you enable the creation of an infinite number of applications, each tailored to its user.
This is the ultimate goal of user customization: Each user builds a custom application for himself. With customization, each user sees her own custom version of the underlying application, as shown in Figure This is X-customization: eXtreme customization. And, in all likelihood, rather than each user building a unique application, a single customization will be implemented by many users in the user community.
But you can still consider X-customization as the ultimate goal of Web development — empowering your users with the tools they need to meet their goals. You probably already recognize that you have different kinds of users in your user community. As a result, you may be planning different parts of your site to serve different user types. As part of this design process, you can create a series of roles for your application, where each role represents a different segment of your user community. The next step is to create a set of controls that can be assembled in different ways to create the pages that make up your application.
You can then go one step further and, after adding your controls to the Web page, use the controls as Web Parts and customize them for various types of users. The final step is to assign users to roles so that when a user logs on, she receives the pages designed for her role.
Creating Your Own Controls Figure Implementing Reusability with Controls One of the key ways to improve your productivity as a developer is through the re-use of components. One example of these kinds of objects is the ADO.
NET objects that are used in. There are two fundamental differences between that definition of objects and the ASP. NET-specific tools custom controls, user controls, and Web Parts. The first difference is that ASP. The ADO. NET objects, for instance, can be accessed from any kind of code. NET objects.
NET environment. The second difference between the ASP. NET tools are used. As part of creating a sales order system, for instance, a developer might create Customer, SalesOrder, and Invoice objects to handle all the activities involved with managing the application data. The ASP. NET tools, however, work only in the presentation layer, where they become an integral part of the user interface.
In terms of the three benefits of using objects productivity, standardization, simplification , the ASP. In addition, middle-tier objects can execute only on the Web server and are accessible only from application code that executes on your Web server. Web Parts, on the other hand, support writing code that will execute on both the server and in the browser, on the client.
With the ASP. NET tools you can include client-side code that will be added to the HTML page that goes to the user and, as a result, executes in the Web browser. Controls in Action As an example of using controls to implement reusability in a user interface, consider a Web-based application that allows users to browse a catalog of books and order books. A user can log on to the application, search for a book by a list of criteria, read the details on a book, and place an order.
Before they can access the site, users must register by entering information about themselves. In addition, when buying a book a customer must enter or review and confirm her billing, shipping, and contact information. In this book application, users can list books in many different ways for instance, wish lists, gift registries, recommended books, reminder lists.
The application supports a variety of different customer types such as individual consumers, companies, or libraries and a variety of ways for customers to order books individual orders, bulk orders, recurring orders, and so on. Customers can buy books using several different purchase mechanisms such as credit card, check, or purchase orders. Refer to Figure to see typical pages from the bookstore Web site, listing books that match criteria entered by the user.
Creating Your Own ControlsObviously, many components in this application can be written once and then reused. However, this code is implementingbusiness rules and should be put in an object that is accessed by the parts of the application that need thatprocessing. Each page, for instance, should have the company logo and basic page information atthe top.
A Web Part would allow you to build a standard title bar for the top of the page that a user could modifyto include their name as they want to be addressed and that would be automatically reloaded when thatuser returns to the site. Figure shows the title barsfrom two different pages implemented through the same Web Part. Figure While the application allows many different ways to list books, the way each book is displayed should bethe same.
By standardizing the way that book information is displayed, users can quickly figure out whereto find particular information such as price, genre, and so on. Because most of the book information is kept ina database, this Web Part could include the code to gather the information from the database either directlyor by interacting with a middle-tier object.
Becausethe detailed information control would use a wide variety of controls and formatting to display all theinformation, it might be easiest to create it as a user control. Because the summary information controlrequires fewer controls, it might be easily created as a custom control. Figure shows examples ofthese two Web Parts, with the detailed display above the summary display. Chapter 1 Figure Creating Your Own Controls It would be convenient to have a single control that could be used to build all lists. This control could be implemented in one of two ways: it could either accept a set of criteria for building a list of books, or accept a list of book identifiers generated by some other part of the application.
Either way, this control would use the previously created book summary control to display the individual books on the list in a standard way. Throughout the application, customer information is gathered and displayed. Rather than make the user work with several different formats for customer information, why not create a customer information control? This control would work in two modes: data entry and data display.
In the data display mode, the control would retrieve customer information from the database and display it. In the data entry mode, the control would accept customer information and do whatever validation is appropriate to the user interface ensuring the customer phone number is correctly formatted, for instance. Once the data has passed validation, the control would take care of updating the database. Here is a case where using a Web Part would be the best choice. If, for example, the Web page contains multiple controls, how do these controls commit their changes to the database? For instance, the Web page that lets customers order books might contain the control for entering customer information and a control for entering sales order header information.
If these controls interact with the database or middle-tier objects individually, processing updates from the page could be very inefficient. The code on the Web page could coordinate updates from the controls, but then you lose some of the benefits of reusability that controls are supposed to provide. By taking advantage of the capability of Web Parts to find other Web Parts on the page and communicate with them, the code in the Web Parts could coordinate their updates. Figure shows the customer information Web Part in two different pages.
From that point on, the developer just needs to add any specialized code required by the particular page. In this section, I offer guidelines on when you should use a user control or a custom control remember that Web Parts are just a kind of custom control. Nothing is free, of course, so you also see what costs you incur by picking each type of control. This also lets you take advantage of Visual Studio. Because a user control can be used in a single project only, if you want to use a user control on two Web sites, you have to copy the control from the project that it was created in and paste it into the other project that it will be used in.
This means that you will eventually end up with multiple copies of the same user control. As a result, the functionality embedded in the user control will start to work differently in the different versions. The second loss occurs because developers will, eventually, have to take time to reconcile the different versions of the user control. For instance, when a bug is discovered, or a change is required because of changes in the organization, or the opportunity for an enhancement is recognized, developers will have to chase down all the versions of the control to make the change.
When to Use a Custom Control Custom controls can do anything that a user control can do — it just takes longer to build them. This makes custom controls an excellent choice if you are paid by the hour. When building a custom control you may find that you have to write the server-side code that generates the client-side portion of your Web Part, including any HTML. This means that you give up the way that ASP.
You can still use ASP. NET WebForm controls in your Web custom control, but you must write code to add those WebForm controls to your user interface rather than using drag-and-drop. The same is true of any client-side code that you add to your Web Part: The client-side code must be generated from your server-side code, rather than typed into the HTML view of a Web Page. Without the benefit of the Visual 15 Chapter 1 Studio. And, without the benefit of Visual Studio.
You should use a custom control only if there is some compelling reason for not using a user control. In the days of the ASP. However, with ASP. The WebPartZone is the second control or multiple controls for multiple zones to add to a page when taking advantage of Web Parts. A WebPartZone has the following syntax:. The WebPartZone control is used to define a region or zone of a page, and it may contain other controls as the example in Listing A demonstrates by including an ASP.
The example includes three zones that are presented on the page via an HTML table the second zone includes the button control. While this is a very simple example, you can place any ASP. NET server control within these zones. One of the simplest ways to use controls is through the Visual Studio development environment. The Toolbox area within Visual Studio when developing Web Forms includes a WebParts category that contains the various controls available to utilize Web Parts in an application.
Two of the more important controls include the following:.
There are numerous additional controls available, but they are beyond the scope of this article. In addition, you may develop your own custom controls as well. These controls may be used to control various aspects of a page utilizing Web Parts. The controls available with ASP. NET allow you to utilize these modes in your application depending upon the requirement. Web Parts have been a standard feature of the SharePoint environment for some time, but they are now available to all ASP.
These controls allow you to easily build dynamic, customizable user interfaces with plenty of options for the users. There are various controls available to integrate Web Parts in your application, but you can develop your own custom controls as well.