绑定一个具有 List<> 类型属性的对象到中继器

发布于 2024-10-08 01:44:27 字数 518 浏览 0 评论 0 原文

我在业务层创建了两个类。

第一个称为 Users,具有 id (int)、pass (string) 和特权 (Privilege) 属性,第二个称为 Privilege,具有 id (int) 和privilegeName (string) 属性。

我有一个返回所有用户的方法,并且我使用了转发器(实际上我将其绑定到 DataList 以自动为我创建 ItemTemplate,然后使用转发器),它可以正常工作并显示除我的列表之外的所有属性财产。它会生成类似 System.Collections.Generic.List`1[WebApplication2.Public.BLL.Users]

我想以友好的方式显示它,例如“用户权限:Privi1,Privi2”,但我仍然想保留图层我的应用程序干净且结构化,例如,我不会将它们存储在同一个表的数据库中,而只是将它们存储为文本并附加它。

我希望找到一个简单而好的解决方案...提前感谢大家=)

PS:我不想显示对象Privilege,我想显示privilege.privilegeName

I've created two classes in business layer.

the first one is called Users with id (int), pass (string) and privileges (Privilege) properties and the second one is called Privilege and has id (int) and privilegeName (string) properties.

I've a method that returns all the users, and I use a repeater (actually I bind it to a DataList to auto create the ItemTemplate for me and then use a repeater) and it works and displays all the properties well except for my List property. it generates instead something like this System.Collections.Generic.List`1[WebApplication2.Public.BLL.Users]

I want to display it in a friendly way like "User Privileges : Privi1, Privi2" but still I want to keep the layers of my application clean and structured, for example I won't store them in a database in the same table and just store them as a text and append it.

I hope to find a simple and good solution...Thanks in advance guys =)

PS : I don't want to display the object Privilege, I want to display privilege.privilegeName

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

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

发布评论

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

评论(2

挽清梦 2024-10-15 01:44:27

使用中继器时,有两种方法,一种是 Bugai13 建议的方法:使用自定义属性来显示它。这对于某些类型的嵌套数据来说很好。

您的另一个选择是在转发器内放置一个转发器,并将其适当绑定(绑定到分配给主数据对象的列表,具体取决于 O/R 映射器的工作方式)。

您可以将自定义显示属性的代码不在数据模型中,而是在表示层中的某个位置(取决于您的框架/设计),因此这样做并不是一件“坏”事。这取决于你,“感觉”最好的。

When using repeaters, there are two approaches, one is the one suggested by Bugai13: to have a custom property that displays it. This is fine for certain types of nested data.

Your other option is to just have a repeater inside a repeater, and bind it appropriately (to what would be a list assigned to your main data object, depending on how you O/R Mapper works).

You can have the code for the custom display property not in the data model, but in your presentation layer somewhere (depending on your framework/design), so it's not a "bad" thing to do that. It's up to you, with whatever "feels" best.

月下客 2024-10-15 01:44:27

只需在您的 Business 对象中创建属性并绑定它:

public string PrivilegiesString
{
   get
   {
     var sb = new StringBuilder("User Privileges : ");
     foreach(var item in privileges)
     {
       sb.AppendFormat("{0}, ",item.privilegeName);
     }

     return sb.ToString();
   }
}

Just create property at your Bussiness object, and bind it:

public string PrivilegiesString
{
   get
   {
     var sb = new StringBuilder("User Privileges : ");
     foreach(var item in privileges)
     {
       sb.AppendFormat("{0}, ",item.privilegeName);
     }

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