将 ASP.NET 中继器与对象属性结合使用
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用内置的
ObjectDataSource
为每个项目映射一个单独的数据源可能是唯一直接的方法(也是唯一足够简单值得付出努力的方法......)。是否要求您使用
ObjectDataSource
,或者您可以选择其他方式从存储中获取数据? 我建议使用实体框架(恕我直言,这很棒)或创建您自己的自定义类型,您可以使用自定义设计的 DAL 获取数据(这比使用 EF 需要更多工作,但如果您像某些人一样担心EF 仍处于起步阶段,这可能是您的选择)。无论哪种情况,您最终都会得到一个名为
Program
的 C# 类,该类具有IEnumerable
类型和IEnumerable
类型的属性> 分别称为利益相关者
和结果
。 然后,您可以将它们用作项目重复器的数据源,并在ProgramRepeater
的ItemDataBound
事件中设置它们,也许是这样的:这假设您正在使用 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 typeIEnumerable<Stakeholder>
andIEnumerable<Outcome>
calledStakeholders
andOutcomes
respectively. You can then use these as datasources for the item repeaters and set them in theItemDataBound
event of theProgramRepeater
, maybe something like this: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 theView
as theModel
object, and loop through itsStakeholders
andOutcomes
in a couple of nestedfor
loops directly on theView
.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.