Binding a DataList to an object's method instead of a property
Question
I have a class that looks like this:
public class Person
{
public string Name { get; set; }
public string Thing() {
...
}
...
}
If I have an IList<Person>
that I'm using as a data source for a DataList control, and the DataList looks like this:
<asp:DataList runat="server" RepeatColumns="1" ID="Profiles">
<ItemTemplate>
<%#Eval("Name") %>
</ItemTemplate>
</asp:DataList>
How do I replace the Name
property of the data source with a call to the data source object's Thing()
method?
Solution
<%#((Person)Container.DataItem).Thing()%>
OTHER TIPS
Use properties. The property will be "Name" (or whatever) and the "Getter" on this property will be the method you wish to use to generate the value. While you can call a function from here, I think it would be better design to use a property.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow