Sunday, September 28, 2008
Adding ASP.NET Controls programmatically
If you design ASP.NET Web Controls or Composite Controls, you have designed controls programmatically. This approach is not as easy as using the Visual Designer in Visual Studio. This post explores the use of the "C class", which is part of the COR download. The "C class" is a set of public static methods for adding Controls, managing strings, XML files and more.
Personally, once I started designing Web Parts, I did not want to go back to designing controls using the Visual Designer. I actually find it easier to place controls on the page and use logic to decide what controls are being placed on the page. However, designing Web Parts with standard Web Controls leads to much code. The "C class" allows design of controls with minimum code.
// Let's review how a Label is added to a control:
Label label = new Label();
Label.ID = "ThisLabel";
label.Text = "The name to display";
label.Width = Unit.Pixel(200);
label.CssClass = "AFormatOption";
this.Controls.Add(label);
It takes five statements just to add a simple label. For complex GUI's, it is easy to get lost in the number of lines required. I started thinking: "Why not have a simple method that adds the Label with a number of parameters?". E.g. Let's review what such a method would look like.
// Using a method that does the work of assigning the values to the named properties
Label label = AddLabelMethod("ThisLabel", "The name to display", "AFormatOption", Unit.Pixel(200), this);
The AddLabelMethod takes five parameters: ID, Text, CssClass, Width and an object that the label control is added to. The AddLabelMethod returns the new label object, which typically is not required because all the work is done in the AddLabelMethod.
The C class in COR is a set of helper methods that simplify the addition of Web Controls. To add a label,
// Using the AddLabel method is the C class simplifies programmatic design.
Label label = C.AddLabel ("ThisLabel", "The name to display", "", "AFormatOption", Unit.Pixel(200), this);
Note: ToolTip is added to the list of parameters. It is shown as a blank string in the example.
The C class, over 2000 lines of code at last count, contains a number of useful methods, including:
- AddTable, NewTableRow, NewTableCell
- AddLabel, AddTextBox, AddLinkLabel, and a lot more
- AddHiddenField and other methods to get and set the value of the hidden field
- Xml file management, e.g. GetAttribute, ReadXmlFile
- AddUpdatePanel, AddUpdateProgress, AddTimer (recent additions)
- ... and more
I find using tables to place controls on the page is now simple. E.g
// Add a table to this control
Table table = C.AddTable("table", Unit.Percentage(100), this);
// Add the first Row
TableRow row = C.NewTableRow(table);
// Add the first cell to the Row – it has text and a tooltip and is 100 pixels wide.
C.NewTableCell(row, Unit.Pixel(100), "a name to display", "a tooltip for the cell", "CellFormat");
// Add a second cell to the Row.
TableCell cell = C.NewTableCell(row);
// Add a text box to the second cell
C.AddTextBox("tb2", "", "Enter the name of something", "TextBoxFormat", Unit.Pixel(200), cell);
Using ASP.NET controls, this five-line sample would have taken about 20 lines of code. This approach has clear advantages:
- Reduced footprint – The number of lines makes it easier to understand what is being done.
- It is easier to add Comments that relate to what is being done.
- The C class methods prompt the user for the typically used parameters. It is now easy to identify if any of the typical parameters have been forgotten.
The "C class" is available in the COR download found on Code Plex.
Related posts include: ASP.NET Life Cycle.
Labels: ASP.net
Subscribe to Posts [Atom]
