The explanation below is what I had found out finally.
Basically, the difference between BoundColumn and TemplateColumn is that you have more control over what and how things are rendered in TemplateColumn. For example, you can decide what controls are used when editing. With Bound Column creates column bound to a field in the data source and rendered in a table cell. You can use a cell’s Text property to get whatever text is there. But please note that this only works for Bound Column.
Here is a sample of HTML code:
<asp:BoundColumn DataField="COUNTRY" HeaderText="Country">
<HeaderStyle Font-Size="11px"></HeaderStyle>
<ItemStyle Font-Size="10px"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="Address">
<HeaderStyle Font-Size="11px"></HeaderStyle>
<ItemStyle Font-Size="10px"></ItemStyle>
<ItemTemplate>
<%# Container.DataItem("ADDRESS")%>
</ItemTemplate>
<EditItemTemplate>
<asp:Label id="lblAddress" Text='<%# Container.DataItem("ADDRESS")%>' Runat="server"></asp:Label>
</EditItemTemplate>
</asp:TemplateColumn>
From the HTML code as above, as you can see, the first bound column will bind the “COUNTRY” columns to its data field, while the second template column will use a Label control to bind the value into it.
Protected Sub GetColumnValue()
'Bound Column... works
Label1.Text = e.Item.Cells(0).Text
'Template Column...doesn't work
Label2.Text = e.Item.Cells(1).Text
End Sub
This is because .NET sees the contents inside the Bound Column as Text and the contents inside the Template Column as a DataBoundLiteralControl (a collection of server controls).
So, to get the value of the second column, you would have to access the DataBoundLiteralControl’s Text property by using FindControl method. Here is the sample code:
Protected Sub GetColumnValue()
'Bound Column...works
Label1.Text = e.Item.Cells(0).Text
'Template Column...works
Label2.Text = CType(e.Item.FindControl("lblAddress"), Label).Text
End Sub
0 comments:
Post a Comment