Thursday, September 07, 2006

Accessing Text in DataGrid's TemplateField

If you try to access the content of TemplateField

<asp:TemplateColumn>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"au_fname") %>
<%# DataBinder.Eval(Container.DataItem,"au_lname") %>
</ItemTemplate>
</asp:TemplateColumn>

using DataGrid1.Items[0].Cells[0].Text (I assume it is first column), you would probably get nothing. It is because the text would be placed in a special literal control called DataBoundLiteralControl, which is auto-generated by the compiler IF you place the data-bound expression in the ItemTemplate. Therefore, to access its content. You can use this,

((DataBoundLiteralControl)dgrdAuthors.Items[0].Cells[0].Controls[0]).Text


You get it !!!

No comments: