Creating Web Server Control Templates Dynamically
In this demo I create classes that implement the ITemplate interface of the
System.Web.UI namespace.
public
class FormattedTemplate
: ITemplate, INamingContainer
{
System.Web.UI.WebControls.ListItemType
_type;
DataColumn
_dc;
public
FormattedTemplate(System.Web.UI.WebControls.ListItemType type,
DataColumn dc)
{
_type = type;
_dc = dc;
}
public
void InstantiateIn(System.Web.UI.Control
container)
{
//implementation
}
}
I then programmatically add
TemplateFields to the GridView based on the attributes of DataColumns that are bound
to those TemplateFields. Observe the following features in this demo:
- If a column holds data of type string then the template will display a TextBox upon
edit and label during display. The size of the displayed field will equal the number
of characters multiplied by 6 pixels.
- If a column holds numeric data, then, upon Edit, a TextBox with both validators
for both the range of the data type and for required field if the default value
on the field cannot be null.
- If a column holds Boolean information then, upon edit, a radiobuttonlist of Yes and No will be displayed
- If a column is read-only then no edit textbox will
be displayed.
- The styles of each column would change based on the attributes; for example primary keys will
be in maroon color.
- Each GridView cell will display the column name, datatype and size when hovered
over it.
The GridView also implements sorting. It also allows multiple rows selection
for deletion.