The
Creating Templates
A TemplateField object enables you to specify templates that contain markup and controls to customize the layout and behavior of a column in a GridView control. Using an
Your template can contain markup, Web server controls, and command buttons. For more information on templates, see ASP.NET Web Server Controls Templates.
Data-Binding in a Template
In a template, you can bind controls to data using the Eval and Bind methods. You use the Eval method when the control will only display values. You use the Bind method when users can modify a data value—that is, for data-update scenarios. You can use the Eval method in any of the templates to display data. You use the Bind method in a template with controls in which users might change values, such as
Example
The following example shows the
Visual BasicВ | Copy Code |
---|---|
<Columns> <asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" ReadOnly="true"/> <asp:BoundField DataField="FirstName" HeaderText="First Name"/> <asp:BoundField DataField="LastName" HeaderText="Last Name"/> <asp:TemplateField HeaderText="Birth Date"> <ItemTemplate> <asp:Label ID="BirthDateLabel" Runat="Server" Text='<%# Eval("BirthDate", "{0:d}") %>' /> </ItemTemplate> <EditItemTemplate> <asp:Calendar ID="EditBirthDateCalendar" Runat="Server" VisibleDate='<%# Eval("BirthDate") %>' SelectedDate='<%# Bind("BirthDate") %>' /> </EditItemTemplate> </asp:TemplateField> </Columns> |
C#В | Copy Code |
---|---|
<Columns> <asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" ReadOnly="true"/> <asp:BoundField DataField="FirstName" HeaderText="First Name"/> <asp:BoundField DataField="LastName" HeaderText="Last Name"/> <asp:TemplateField HeaderText="Birth Date"> <ItemTemplate> <asp:Label ID="BirthDateLabel" Runat="Server" Text='<%# Eval("BirthDate", "{0:d}") %>' /> </ItemTemplate> <EditItemTemplate> <asp:Calendar ID="EditBirthDateCalendar" Runat="Server" VisibleDate='<%# Eval("BirthDate") %>' SelectedDate='<%# Bind("BirthDate") %>' /> </EditItemTemplate> </asp:TemplateField> </Columns> |