Browse DevX
Sign up for e-mail newsletters from DevX


ASP.NET: Extending the Power of Web Pages with User Controls

User controls represent an interesting and powerful level of page reusability in ASP.NET. You can build user controls by extracting and combining blocks of code from existing pages. You can also assemble ASP.NET controls together in a sort of embeddable form for use in Web pages. In this article, you'll get a crash-course on how to design, write and use ASP.NET user controls.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

hen designing the new version of ASP, Microsoft put a lot of effort into the individuation of a powerful and effective extensibility model. The Extensible Stylesheet Language (XSL) was the first option considered. For some time, the XML/XSL pair looked like the natural way to extend and improve ASP. You describe the page content using XML and render it to a potentially infinite number of target devices using XSL transformations (XSLT). Clearly in this way the core of your code would have been expressed with XSLT instructions, thus making the throughput of the XSLT layer an extremely critical factor capable of affecting the response time and the performance of the server.

Planned for use on a large scale, the problems with XSLT can be narrowed down to two areas. The first area deals with the quirkiness of XSLT syntax. Maintenance certainly becomes a major issue if not a nightmare as soon as you base your code all on XSLT. Secondly, the inability for users to control exactly what the XSLT processor is doing is another problem.

As a result, Microsoft discarded the XSLT option. Instead, they redesigned the ASP environment making it completely component-based. In doing so, they killed two birds with one stone. For one thing, they integrated ASP.NET with the surrounding .NET environment and framework. In addition, they came up with a stable and consolidated model for extending applications—using inheritance and other object-oriented programming techniques. Enter ASP.NET and its families of controls!

Fast Facts
The extensibility model is key to the success and the rapid adoption of a software technology. In ASP.NET, the extensibility model, as well as the programming model, is totally based on components. ASP.NET components and applications are fully integrated with the .NET platform and benefit from a number of OOP-related facilities. Code-behind and user controls are just some of the smartest.

New Controls in ASP.NET
At the highest level of abstraction, ASP.NET provides two ways to create new and custom controls. You can create a new control class inheriting from a base class (i.e., WebControl) or you can group a few controls together in a sort of embeddable child page. Although the second approach can appear not particularly object-oriented at first, the class you come up with is built atop the base UserControl class. The first approach is referred to as building custom controls. The second type of controls are said to be user controls or pagelets.

A custom control is primarily a class built to behave like a control and provide a given set of functionalities, such as a textbox or drop-down list. A user control is a form made of any combination of server and client controls sewn together with server and client script code. A user control has a rich user interface but is programmable as a monolithic component. In other words, like a Visual Basic ActiveX control, all of its constituent controls are protected and inaccessible if not through a public programming interface.

An embeddable Web form is the definition that best fits what a user control actually is. You create a user control in much the same way you create a Web form. When you're done with code and layout, you give the resulting file an .ascx extension. This enables you to use the control with any ASP.NET page. Pages see it as a monolithic component and work with it as with any other system Web control. If you are familiar with Dynamic HTML (DHTML) Scriptlets, you can see that user controls are just their ASP.NET counterpart. (See sidebar "Cascading Stylesheet Support".)

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date