Advertisment

Web Development with WebForms

author-image
PCQ Bureau
New Update

ASP to ASP.NET



In the days before .NET, a website grew from a static HTML page rendered by a browser to a set of dynamic pages put together easily and hastily to render dynamic Web pages. ASP pages used the scripting engines on the server to interpret and execute the code. No compilation meant an easy development environment (VisualInterdev helped, though a lot of people used notepad) at the cost of performance. Used in conjunction with COM components, the dotcom boom heralded many applications that used the DNA Framework to Web-enable their applications successfully, including Commerce Server from Microsoft.

Advertisment

The advent of the .NET Framework and ASP.NET takes Web development to the next level. To begin with,

ASP.NET:

  • is built into the .NET Framework and is controlled by CLR (Common Language Runtime).
  • supports the various languages supported on the .NET Framework.
  • is object-oriented. 
  • is compiled code. The ASPX file is actually compiled into an assembly as native code and executed.
  • eases Web development by the introduction of code-behind pages, server-side controls and custom-control development options.
  • has introduced much better security, session state management and caching to improve performance enormously.
  • has introduced a reliable process model to handle out-of process executions, thus relieving IIS of this load.
  • has vastly improved error handling, resulting in clean applications and easy debugging.

Let's see, in more detail, at what developers have in store.

Advertisment

ASP.NET code



ASP.NET allows development in any of the .NET languages, including C# and VB.NET. When a browser client requests .ASPX resources, the ASP.NET runtime parses and compiles the target file into a .NET framework class. This class can then be used to dynamically process incoming requests. (Note that the .ASPX file is compiled only the first time it is accessed; the compiled type instance is then reused across multiple requests.)

For backward compatibility with ASP, ASP.NET provides support for <% %> code render blocks that can be intermixed with HTML content within an .ASPX file. These code blocks execute in a top-down manner at page render time. But, unlike ASP, the code used within the above <% %> blocks is actually compiled and not interpreted using a script engine. This results in improved runtime execution performance for the ASP pages just by renaming the page extensions to

.ASPX.

Remember the client server days of developing Win apps using VB Forms? You could create a form, drop controls on to it, double-click and write code behind it. ASP development changed all that. With intertwined ASP and HTML code, we had to write pages to code to handle the user interface elements and push data from a data store onto them. With ASP.NET, the good old days are back.

Advertisment

Server-side controls



Inherited from the System.Web.UI.WebControl class, the Web controls have a rich object model. When a control is dropped on to a page, it includes the code-behind:

Protected with events TextBox1 As System.Web.UI.WebControls.TextBox 

This creates a reference to the Web control. This declaration also allows you to assign event-handling routines to the control. Now you can use the control in a similar fashion that you were used to with the VB environment. The server-side controls, moreover, have some features that make Web development a breeze. 

Advertisment

The server controls are designed to maintain their state, including any client-entered values between round trips to the server.

This control state is not stored on the server (instead, it is stored within an

form field that is round-tripped between requests). Note that no client-side script is required and the state can also be controlled

programmatically.

The ASP.NET page has a set of events that are triggered. This model allows the page to retain its state and to handle the post-back of an ASP.NET page automatically. Server-side controls leverage this model to maintain their state between server trips, thus giving a seamless user interface.

Advertisment

The server controls have the ability to detect the browser version and render the relevant output. For all of us who have been fighting to make our sites cross-browser compatible (though I can't understand why, when IE has over 96% of the desktop market), it reduces the duplication in effort. 

The Web Forms Framework includes a set of validation-server controls that provides a powerful way to check input forms for errors and, if necessary, display messages to the user. There are controls for specific types of validation like range checking or pattern matching, plus a RequiredFieldValidator that ensures that a user does not skip an entry field. Added to a WebForms page like other server controls, multiple validation controls can be attached to an input control. For example, it is

possible to specify both that an entry is required and that it must contain a specific range of values. 

Each ASP.NET server control is capable of exposing an object model containing properties, methods and events. ASP.NET developers can use this object model to modify and interact with the page. Used in conjunction with the ASP.NET page lifecycle, this provides a rich event model for the developer.

Advertisment

There are 45 built-in server controls that ship with the .NET Framework. In addition to the rich set of Web-controls that are available out of the box, ASP.NET allows the development of a user control. Of the System.Web.UI.UserControl type, which inherits from System.Web.UI.Control the user control can be used to aggregate a set of Web controls with their behavior into a single reusable control. The .ASCX extension is used to indicate such controls. This ensures that the user controls file cannot be executed as a standalone Web-forms page (any Web-forms page can be used as a user control). The user controls are included in a Web-forms page using a Register directive: 

<%@ Register TagPrefix="MyCtrl" TagName="Login" Src=""/MyApp/Security/login.ascx" %>

The TagPrefix determines a unique namespace for the user control (so that multiple user controls with the same name can be differentiated from each other). The TagName is the unique name for the user control and the Src attribute is the virtual path to the user control. After registering the user control, you may place the user control tag anywhere in the Web Forms page. For example:

Advertisment

 



ASP.NET caching

The .NET Framework provides multiple options for caching a Web page. A dynamic page can be cached at the Web-server level. It is also possible to cache fragments of a page. The caching can be specified for a certain period of time and can be based on parameters. For example, if a search for all leave requests for the current day is fired, then this information is cached. But any search with a different date will trigger the query against the data base and not take data from the cache. It is possible to cache a full page or a fragment of a page. It is also possible to programmatically set caching using a cache.insert command.

The cached data can be configured to decay. This ensures that over a period of time, unused data will decay and can thus be removed from the cache.

ASP.NET scalability



ASP.NET applications support three configurations for maintaining sessions. With the state as "InProc", the session is maintained in the IIS memory space. This is similar to hosting ASP on IIS. With "StateServer", the session can be maintained in a separate memory space on any machine running the state server. With this, multiple Web servers can share the session information from a single server. This enables the application to be scaled into a Web farm with minimum changes. The third option is "SQLServer", in which the session information is persisted into an SQL Server DB. While this option is costlier, it ensures that even a reboot of the machine continues to maintain the state information.

WinForms in Webpages



In addition to the vast improvements in ASP.NET, the .NET Framework allows the hosting of WinForms within a webpage. This requires the .NET runtime installed on the client. But in exchange for the heavy installation (WinXP and above provide the .NET Runtime as part of the OS), it provides the richness of the Windows application to the Web. It also allows for the code to be downloaded incrementally and the WinForms application to cache the data locally. This reduces the round trips to the server, thus improving the performance while providing for the rich GUI of the thick client applications.

ASP.NET has a lot of benefits over ASP. While it supports a direct port of ASP pages onto ASP.NET, it has introduced the development of compiled ASP.NET pages in any of the .NET languages. Web development is now cleaner with the code-behind structure of

ASP.NET. 

Server-side controls, used in conjunction with the ASP.NET page life-cycle, provide cross-browser compatibility and a rich event model that allows the page to maintain its state across round trips to the server. It also allows the developer to wrap common functions into user-controls, which are very easy to develop.

The rich caching and session state management allows ASP.NET application to scale to large Web farms leading to the best performance for enterprise Web development. The improved error handling provides a robust structure for error trapping and provides vastly improved debugging.

Praveen Srivatsa, MSDN Regional Director, Bangalore

Advertisment