简体   繁体   English

将ASP.NET中继器与对象属性一起使用

[英]Using ASP.NET Repeaters with Object Properties

I'm trying to use ASP.NET's Repeater objects to loop over properties of an object. 我正在尝试使用ASP.NET的Repeater对象来遍历对象的属性。

For example... I have an ObjectDataSource to grab object "Program" by ID... 例如...我有一个ObjectDataSource可以通过ID来获取对象“ Program” ...

Program has properties such as Program.Stakeholders and Program.Outcomes which are Lists of "Stakeholder" and "Outcome" objects. Program具有诸如Program.Stakeholders和Program.Outcomes之类的属性,它们是“利益相关者”和“结果”对象的列表。

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? 任何人都可以提供一种更好的方式来使用这些Repeater对象,或者向我指出一些会有所帮助的资源吗? If this doesn't make sense I can try to clarify it more. 如果这没有意义,我可以尝试进一步澄清。

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...). 使用内置的ObjectDataSource为每个项目映射一个单独的数据源可能是唯一的直接方法(而且唯一的方法很容易值得付出努力...)。

Is it a requirement that you use the ObjectDataSource , or can you choose a different way to get the data from the storage? 是使用ObjectDataSource的要求,还是可以选择其他方法从存储中获取数据? 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). 我建议您使用Entity Framework(imho摇动)或创建您自己的自定义类型,以使用自定义设计的DAL向其获取数据(这比使用EF的工作要多得多,但是如果您像某些人那样担心EF仍处于起步阶段,这可能是您的选择)。

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. 无论哪种情况,您都将得到一个名为Program的C#类,该类具有IEnumerable<Stakeholder>IEnumerable<Outcome>类型的属性,分别称为StakeholdersOutcomes 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: 然后,您可以将它们用作重复项的数据源,并在ProgramRepeaterItemDataBound事件中进行ProgramRepeater ,也许是这样的:

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


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. 只是为了让您了解使代码执行什么操作-不一定是解决问题所需的确切代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM