绑定列表到中继器
我有一个复杂的类,类似于:
public class Person
{
public int Pid;
IList<Address> Addressess;
public Name Name;
public Name PartnerName;
Person(int id)
{
Addressess = new List<Address>();
}
}
public class Address
{
public string HouseName;
public string street;
public string country;
public string universe;
public string galaxy;
}
public class Name
{
public string Firstname;
public string Lastname;
public string Fullname { get { return Firstname + " " + Lastname; } }
}
所以,现在,当我像这样绑定转发器时:
rpPeople.DataSource = PeopleNearYou; //this is a List<Person>();
在实际的转发器中,我想显示详细信息。要访问,例如,Pid
,我需要做的就是:
<%# Eval("Pid") %>
现在,我不知道如何访问中继器中的全名
<%# Eval("Fullname") %> //error, fullname not found
此外,我只想仅显示第一个地址 我做不到那么
<%# Eval("Address").First().Universe %> //red, glarring error. can't figure out how
,我该如何显示这些东西呢?
非常感谢。
I have a complicated class which is something like:
public class Person
{
public int Pid;
IList<Address> Addressess;
public Name Name;
public Name PartnerName;
Person(int id)
{
Addressess = new List<Address>();
}
}
public class Address
{
public string HouseName;
public string street;
public string country;
public string universe;
public string galaxy;
}
public class Name
{
public string Firstname;
public string Lastname;
public string Fullname { get { return Firstname + " " + Lastname; } }
}
So, now, when I bind the repeater like so:
rpPeople.DataSource = PeopleNearYou; //this is a List<Person>();
and in the actual repeater, I want to show the details. To access, say, Pid
, all I need to do is:
<%# Eval("Pid") %>
Now, I can't figure out how to access the full name in repeater
<%# Eval("Fullname") %> //error, fullname not found
Also, I want to display only First Address only and I can't do that
<%# Eval("Address").First().Universe %> //red, glarring error. can't figure out how
So, how would I display these stuff please?
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您在绑定中继器时获取所需的类成员,这会容易得多。
现在您需要在中继器中做的就是:
This will be so much easier if you grab required class members when you bind the repeater.
Now all you need to do in your repeater is:
如果我遇到这样的复杂情况,我总是使用 ItemDataBound 事件,因为您可以获得更多控制权。例如,在您的情况下,我将在项目模板中创建一个标签,将 ItemDataBound 绑定到类似于此的代码...
如果您有页眉/页脚行,您还需要检查 e.Item.Type。
If I get in to complicated situations like this I always use the ItemDataBound event as you can get much more control. For example, in your situation I would create a label in the item template, bind the ItemDataBound to code similar to this...
You'll need a check on e.Item.Type too if you have header/footer rows.