Edit: My object is defined as below. I passed it into GridView1.
public class UserTestorViewModel
{
public string Username {get;set;}
public string Password {get;set;}
public Label SuiteName {get;set;}
}
I have GridView bound to a customized List<UserTestorViewModel>:
this.GridView1.DataSource = utViewModelList;
this.GridView1.DataBind();
and in the .aspx I have
<asp:TemplateField HeaderText="LoginName">
<ItemTemplate>
<asp:Label ID="LoginName" runat= "server" Text= '<%# ((ViewModels.UserTestorViewModel)Container.DataItem).User.userName %> '></asp:Label>
</ItemTemplate>
</asp:TemplateField>
this works because ViewModels.UserTestorViewModel.User.userName is string, but
<asp:TemplateField HeaderText="SuiteName">
<ItemTemplate>
<%# ((ViewModels.UserTestorViewModel)Container.DataItem).SuiteName %>
</ItemTemplate>
</asp:TemplateField>
because ViewModels.UserTestorViewModel.SuiteName is Label from System.Web.UI.WebControls
So how to bind a System.Web.UI.WebControls to <ItemTemplate> NOT the Text of the System.Web.UI.WebControls
You may place a Placeholder control in ItemTemplate and put your label control to it on the RowCreated event of the GridView: