One advantage of a template is that it allows the use of data binding expressions, which greatly extends the format and representation of the Data Binding method.
A key technology used in many scenarios is to add a special method to the page class to process field values. In this way, it breaks through the restrictions of simple data binding and allows you to add dynamic information and conditional logic.
For example, you want to create an icon column on the edge of each row, and select the most suitable image based on the data in the row. The following shows an example. When the inventory of a project is greater than 50, a checkmark is displayed. When the inventory is 0, a cross sign is displayed.
< ASP: templatefield Headertext = "Status" >
< Itemtemplate >
< IMG SRC = '<% # Getstatuspicture (container. dataitem) % > 'Alt = "status">
</ Itemtemplate >
</ ASP: templatefield >
The getstatuspicture () method checks the data items and selects the appropriate image URL.
Protected String Getstatuspicture ( Object Dataitem)
{
Int Units = Int32.parse (databinder. eval (dataitem, " Unitsinstock " ). Tostring ());
If (Units = 0 )
{
Return "Cancel.gif";
}
Else If (Units > 50 )
{
Return "OK .gif";
}
Else
{
Return "Blank.gif";
}
}