绑定列表到中继器

发布于 2024-11-28 02:28:55 字数 1219 浏览 2 评论 0原文

我有一个复杂的类,类似于:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞美少年 2024-12-05 02:28:55

如果您在绑定中继器时获取所需的类成员,这会容易得多。

rpPeople.DataSource = PeopleNearYou.Select(r => new
      {
           Pid = r.Pid,
           Universe = r.Addressess.First().Universe,
           Fullname = r.Name.Fullname
      }

现在您需要在中继器中做的就是:

<%# Eval("Universe") %>
<%# Eval("Fullname") %>

This will be so much easier if you grab required class members when you bind the repeater.

rpPeople.DataSource = PeopleNearYou.Select(r => new
      {
           Pid = r.Pid,
           Universe = r.Addressess.First().Universe,
           Fullname = r.Name.Fullname
      }

Now all you need to do in your repeater is:

<%# Eval("Universe") %>
<%# Eval("Fullname") %>
风尘浪孓 2024-12-05 02:28:55

如果我遇到这样的复杂情况,我总是使用 ItemDataBound 事件,因为您可以获得更多控制权。例如,在您的情况下,我将在项目模板中创建一个标签,将 ItemDataBound 绑定到类似于此的代码...

void rpt1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    ((Label)e.Item.FindControl("lblFullName")).Text = ((Person)e.Item.DataItem).FullName;
}

如果您有页眉/页脚行,您还需要检查 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...

void rpt1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    ((Label)e.Item.FindControl("lblFullName")).Text = ((Person)e.Item.DataItem).FullName;
}

You'll need a check on e.Item.Type too if you have header/footer rows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文