If I have a List < Person > where person is defined by the class
如果我有一个List
class Person
{
string Forename
{
get;set;
}
string Surname
{
get; set;
}
}
And I bind it to an asp repeater control that looks like this:
我将它绑定到一个asp转发器控件,如下所示:
<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
我认为数据绑定的全部意义在于,这是有效处理的,但是当我检查Repeater1.Items集合时,没有进行任何更改。我是否必须编写代码来执行某些操作
//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?
如果是这种情况,为什么DataItem属性总是为空?
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?
我尝试过使用Bind(“Forename”),但这似乎没有将TextBox中的信息带回对象,我是否必须手动执行此操作?
3 个解决方案
#1
8
The simple answer is that the Repeater control does not support the kind of two-way databinding that you are looking for. On top of that, the DataItem property is only used during the creation of the repeater item, and after the ItemDataBound event, it is set to nothing. So you cannot use that property to get the original object you used when creating the specific repeater item after postback (as you are doing in your pseudocode).
简单的答案是Repeater控件不支持您正在寻找的那种双向数据绑定。最重要的是,DataItem属性仅在创建转发器项期间使用,并且在ItemDataBound事件之后,它被设置为空。因此,您无法使用该属性来获取在回发后创建特定转发器项时使用的原始对象(正如您在伪代码中所做的那样)。
You will have to loop through the repeater items, as you suggested (make sure to check that the item is of ListItemType.Item or AlternatingItem before doing anything) and then extract the values from the textboxes and use them in an update.
您必须按照建议循环遍历转发器项(确保在执行任何操作之前检查该项是ListItemType.Item或AlternatingItem),然后从文本框中提取值并在更新中使用它们。
#2
7
If you Bind the Repeater with the person list you want like
如果您将Repeater与您想要的人员列表绑定在一起
this.Repeater1.DataSource = GetPersons();
while GetPersons() is a method returning a list of person objects you could use
而GetPersons()是一个返回您可以使用的人物对象列表的方法
<asp:TextBox ID="txtForename" runat="server" Text='<%# Eval("Forename") %>' />
#3
2
In addition to the above, you also need to bind the repeater to the List. Right now the text boxes are assigned to the value of the Forename (or potentailly bound if you use the
除上述内容外,还需要将转发器绑定到List。现在,文本框被分配给Forename的值(或者如果您使用的话,则被强制绑定
<# Bind("Forename") %>
tag), but the Repeater Container has no DataItem.
标签),但Repeater容器没有DataItem。
#1
8
The simple answer is that the Repeater control does not support the kind of two-way databinding that you are looking for. On top of that, the DataItem property is only used during the creation of the repeater item, and after the ItemDataBound event, it is set to nothing. So you cannot use that property to get the original object you used when creating the specific repeater item after postback (as you are doing in your pseudocode).
简单的答案是Repeater控件不支持您正在寻找的那种双向数据绑定。最重要的是,DataItem属性仅在创建转发器项期间使用,并且在ItemDataBound事件之后,它被设置为空。因此,您无法使用该属性来获取在回发后创建特定转发器项时使用的原始对象(正如您在伪代码中所做的那样)。
You will have to loop through the repeater items, as you suggested (make sure to check that the item is of ListItemType.Item or AlternatingItem before doing anything) and then extract the values from the textboxes and use them in an update.
您必须按照建议循环遍历转发器项(确保在执行任何操作之前检查该项是ListItemType.Item或AlternatingItem),然后从文本框中提取值并在更新中使用它们。
#2
7
If you Bind the Repeater with the person list you want like
如果您将Repeater与您想要的人员列表绑定在一起
this.Repeater1.DataSource = GetPersons();
while GetPersons() is a method returning a list of person objects you could use
而GetPersons()是一个返回您可以使用的人物对象列表的方法
<asp:TextBox ID="txtForename" runat="server" Text='<%# Eval("Forename") %>' />
#3
2
In addition to the above, you also need to bind the repeater to the List. Right now the text boxes are assigned to the value of the Forename (or potentailly bound if you use the
除上述内容外,还需要将转发器绑定到List。现在,文本框被分配给Forename的值(或者如果您使用的话,则被强制绑定
<# Bind("Forename") %>
tag), but the Repeater Container has no DataItem.
标签),但Repeater容器没有DataItem。