将 ASP.NET 中继器与对象属性结合使用

发布于 2024-07-18 04:38:15 字数 376 浏览 6 评论 0原文

我正在尝试使用 ASP.NET 的 Repeater 对象来循环对象的属性。

例如...我有一个 ObjectDataSource 可以通过 ID 获取对象“Program”...

Program 具有 Program.Stakeholders 和 Program.Outcomes 等属性,它们是“Stakeholder”和“Outcome”对象的列表。

现在...我真正想做的是使用中继器来定位这些属性并循环它们包含的列表。 然而,据我所知,我必须为每个列表设置一个单独的数据源,并与检索每个列表的单独方法绑定。

任何人都可以提供更好的方法来使用这些 Repeater 对象,或者向我指出一些有帮助的资源吗? 如果这没有意义,我可以尝试进一步澄清。

I'm trying to use ASP.NET's Repeater objects to loop over properties of an object.

For example... I have an ObjectDataSource to grab object "Program" by ID...

Program has properties such as Program.Stakeholders and Program.Outcomes which are Lists of "Stakeholder" and "Outcome" objects.

Now... what I'd really like to do is use the Repeaters to target these Properties and loop over the lists they contain. However, as far as I know I'd have to set up a separate data source for each one, tied to an individual method to retrieve each list.

Can anyone provide a better way to use these Repeater objects, or point me at some resources which would help? If this doesn't make sense I can try to clarify it more.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

挖鼻大婶 2024-07-25 04:38:15

使用内置的 ObjectDataSource 为每个项目映射一个单独的数据源可能是唯一直接的方法(也是唯一足够简单值得付出努力的方法......)。

是否要求您使用ObjectDataSource,或者您可以选择其他方式从存储中获取数据? 我建议使用实体框架(恕我直言,这很棒)或创建您自己的自定义类型,您可以使用自定义设计的 DAL 获取数据(这比使用 EF 需要更多工作,但如果您像某些人一样担心EF 仍处于起步阶段,这可能是您的选择)。

无论哪种情况,您最终都会得到一个名为 Program 的 C# 类,该类具有 IEnumerable 类型和 IEnumerable 类型的属性> 分别称为利益相关者结果。 然后,您可以将它们用作项目重复器的数据源,并在 ProgramRepeaterItemDataBound 事件中设置它们,也许是这样的:

protected void ProgramRepeater_ItemDataBound(object sender, ItemDataBoundEvent e) {
    Program dataItem = (Program)e.DataItem;
    Repeater stakeholderRptr = (Repeater)e.Item.FindControl("ProgramRepeater");
    Repeater outecomeRptr = (Repeater)e.Item.FindControl("OutcomeRepeater");

    stakeholderRptr.DataSource = dataItem.Stakeholders;
    stakeholderRptr.DataBind();
    outecomeRptr.DataSource = dataItem.Outcomes;
    outecomeRptr.DataBind();
}

这假设您正在使用 ASP当然是.Net WebForms。 在 ASP.Net MVC 中,这甚至更容易 - 您只需将 Program 对象作为 Model 对象发送到 View 对象,然后循环遍历其 <直接在 View 上的几个嵌套 for 循环中的 code>Stakeholders 和 Outcomes


注意:所有代码均按原样提供,我不保证它会按预期运行甚​​至编译。 它只是让您了解如何让您的代码做什么 - 不一定是解决问题所需的确切代码。

Using the built-in ObjectDataSource mapping up a separate datasource for each item is probably the only straightforward way (and the only way that's easy enough to be worth the effort...).

Is it a requirement that you use the ObjectDataSource, or can you choose a different way to get the data from the storage? I would recommend either using Entity Framework (which imho rocks) or creating your own custom types to which you get the data with a custom designed DAL (which is a lot more work than using EF, but if you're, like some, concerned that EF is still in infancy this might be your option).

In either case, you'll end up with a C# class called Program, which has properties of type IEnumerable<Stakeholder> and IEnumerable<Outcome> called Stakeholders and Outcomes respectively. You can then use these as datasources for the item repeaters and set them in the ItemDataBound event of the ProgramRepeater, maybe something like this:

protected void ProgramRepeater_ItemDataBound(object sender, ItemDataBoundEvent e) {
    Program dataItem = (Program)e.DataItem;
    Repeater stakeholderRptr = (Repeater)e.Item.FindControl("ProgramRepeater");
    Repeater outecomeRptr = (Repeater)e.Item.FindControl("OutcomeRepeater");

    stakeholderRptr.DataSource = dataItem.Stakeholders;
    stakeholderRptr.DataBind();
    outecomeRptr.DataSource = dataItem.Outcomes;
    outecomeRptr.DataBind();
}

This is assuming that you're using ASP.Net WebForms, of course. In ASP.Net MVC it is even easier - you just send the Program object to the View as the Model object, and loop through its Stakeholders and Outcomes in a couple of nested for loops directly on the View.


Note: All code is provided as is, and I do not guarantee that it will run as expected or even compile. It is just to give you an idea of what to make your code do - not necessarily the exact code you need to solve your problem.

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