If I have a List < Person > where person is defined by the class
class Person
{
string Forename
{
get;set;
}
string Surname
{
get; set;
}
}
And I bind it to an asp repeater control that looks like this:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Label ID="lblForename" runat="server" Text="Forname" AssociatedControlID="txtForename" />
<asp:TextBox ID="txtForename" runat="server" Text='<%# ((Person)Container.DataItem).Forename %>' />
<br />
<asp:Label ID="lblSurname" runat="server" Text="Forname" AssociatedControlID="txtSurname" />
<asp:TextBox ID="txtSurname" runat="server" Text='<%# ((Person)Container.DataItem).Surname %>' />
<br />
</ItemTemplate>
</asp:Repeater>
What is the best way to get the data that the user types in back into the objects?
I thought that the whole point of data binding was that this was effectively handled for you, but when I inspect the Repeater1.Items collection, there are no changes made. Do I have to write code to do something along the lines of
//This is only intended to be pseudo code
for each item in Repeater1.Items
((Person)item.DataItem).Forename = item.FindControl("txtForname").Text;
end for
If that is the case, why is the DataItem property always empty?
Additional info:
I am already calling code the the effect of
this.Repeater1.DataSource = this.PersonList;
this.Repeater1.DataBind();
I've tried using Bind("Forename")
, but this doesn't seem to bring the info from the TextBox back into the object, do I have to do this manually?