I'm working with Databinding in ASP.Net 2.0, and I've come across an issue with the Eval command.
I have a class that I'm databinding, that looks like this:
public class Address
{
public string Street;
public string City;
public string Country;
public new string ToString()
{
return String.Format("{0}, {1}, {2}", Street, City, Country);
}
}
And another class (the one I'm databinding to):
public class Situation
{
public Address ObjAddress;
public string OtherInformation;
}
Now, when I have a databound control, e.g.
<asp:DetailsView ID="dvSituation" DataSourceID="dataSourceThatPullsSituations" AutoGenerateRows="false"runat="server">
<EmptyDataTemplate>
No situation selected
</EmptyDataTemplate>
<Fields>
<asp:BoundField HeaderText="Other data" DataField="OtherInformation" />
<asp:TemplateField>
<HeaderTemplate>
Address
</HeaderTemplate>
<ItemTemplate>
<%-- This will work --%>
<%# ((Situation)Container.DataItem).ObjAddress.ToString() %>
<%-- This won't --%>
<%# Eval("ObjAddress") %>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
Why doesn't my ToString() class get called when this field is Eval'ed? I just get the type name when that eval runs.