Hello Friends....
Welcome to this Blog...This Blog is specially for the .Net Developers where all developers can discuss the .Net and Sql Articles.
"And will you succeed? Yes! You will indeed!(98 and 3/4 percent guaranteed)."

Sunday, September 12, 2010

Adding Template Fields to a GridView Control.

A TemplateField is used in cases where authors need complete control over the
contents of a GridView column. The TemplateField is declared with an
<ItemTemplate> element, which in turn is used to define the content that will be
rendered for each row in the column. The ItemTemplate can include any valid HTML.

To add a template column:-

1.Set the GridView control's AutoGenerateColumns property to false.

<asp:GridView id="myGrid" runat="server"
   autogeneratecolumns=false
   ...
>
2. Within the GridView declaration, declare a <Columns> element.
3.Within the Columns element, define the <asp:TemplateField> 
control, along with the required <ItemTemplate> element.
4.Within the ItemTemplate, define the HTML or server control that 
will contain the field value.
To display the value of a field in an item template,
use DataBinding 
Expression Syntax
5.Optionally set the <asp:TemplateField> control's 
other properties.
For syntax, see GridView Control Syntax
 
<columns>
 <asp:templatefield>

   <itemtemplate>
     <img width=60 align="top" 
src='<%# Eval ("title_id", "../images/title-{0}.gif" )%>'>
   </itemtemplate>

  </asp:templatefield>
   ...
</columns>

2 comments: