There are several different groups of controls in ASP.Net, ranging from the foundational HtmlControls to the advanced Custom Server Controls:
- HtmlControls - standard HTML controls. (In the System.Web.UI.HtmlControls namespace).
- WebControls - ASP.Net controls. These include controls with similar Html counterparts like Button and Dropdown, but also have advanced controls like Validators and DataGrid (In the System.Web.UI.WebControls namespace).
- UserControls - Combines multiple controls into a single, reusable control (Inherits from System.Web.UI.UserControl class).
- Custom Server Controls
- Composite Custom Controls - create new controls from existing server controls.
- Rendered Custom Controls - create entirely new controls from raw HTML.
- Extended Controls - creates a new server control by inheriting from a previous control.
While everything ultimate reduces to Html, grouping controls together for reusable packages lets us save a lot of time. One such technique is to create an Extended Control by inheriting from an already existing control. This is great for when you want to build off a single control and extend its functionality. For example you could inherit from the TextBox class and add a new method ToLower which makes all the text lowercase.
There are several benefits
- Very easy - just create a new class that inherits from the control you need
- Has all the properties of the base control
- Has Design-time support (unlike User Controls)
This is easy to do, here's how:
- Create a new WebControlLibrary (compiles to a DLL). This is used for Composite and Rendered controls as well.
- Add a new class called TextBoxA, and make it inherit from System.Web.UI.WebControls.TextBox
- Add the methods and properties you need.
using System;using System.Web.UI.WebControls;namespace WebControlLibrary1{ /// |
In the code snippet, we've added a method ToLower which references the textbox property "Text" and makes it lowercase. We've also created an arbitrary property NewProp and made it save data using ViewState.
We can load this easily into Aspx pages by first adding the DLL to the project like any other reference, and then adding the control itself to the toolbox. This lets us perform drag & drop operations like any other WebControl.
- In the ToolsBox, right click for the context menu and select "Add/Remove Items"
- A new window appears with two tabs: (1) .Net Framework Components (selected by default) and (2) COM Components. Within the tab there is a list of controls with checkboxes.
- Click the browse button to find the WebControlLibrary we just created, and add it.
- Select the checkboxes for the controls you want to appear in the ToolBox.
- The controls are now in the toolbox, and you can drag them onto a page just like any other.
If you don't want to add it to your toolbox, then you can also directly type the Html. You need to first Register the assembly by putting something like this at the top:
|
And then reference the control in the page by:
|
In summary, this is a quick technique with high return, and therefore a good skill to have.
No comments:
Post a Comment