【问题标题】:Generate overview of objects with only the selected properties生成仅具有选定属性的对象概览
【发布时间】:2012-10-03 07:47:53
【问题描述】:

我有一个在整个 Web 应用程序中使用的用户控件。它在表中返回 List<Person>。目前列表中显示的属性是固定的(通过代码)。

我想更改控件,使其接受属性列表,然后使用该列表来呈现这些对象(Person 类型)的表,其中只有选定的属性可见。

这只能使用反射吗?实现这一目标的最佳途径是什么。

对象:

Class Person{
  Name{get;set;}
  Age{get;set;}
  Address{get;set;}
  Role{get;set;}
  AnotherProperty{get;set;}
}

用户控件只是获取一个 Person 列表并在一个普通的 html 表中显示结果(通过一个转发器)。 现在我希望用户控件可以自定义,其中的属性显示在这个 html 表中。我希望用户控件按如下方式使用:

<uc:PersonSearchList runat="server" ID="someId" ShowProperties="Name, Address, Role" />

或在后面的代码中设置:

PropertyInfo[] ShowProperties = Person.GetType().GetProperties();

【问题讨论】:

  • 请提供一些代码,以便我们更容易理解您的问题并为您提供帮助
  • 添加了代码。这个问题没有太多真正相关的编码,因为我需要知道如何首先开始

标签: c# ascx


【解决方案1】:

您可以通过几种方式做到这一点。我给你看两个。

首先,您可以在用户控件中创建公共属性。然后,在后面的代码中,将列表添加到属性中,如下所示:

用户控制:

Partial Class user_controls_myControl
    Public newList As List(Of myList)

    'do your other suff
end Class

ASPX 页面:

dim nameOfList as new List(of String)
me.myControl1.newList = nameOfList

或者,您可以将其添加到公共方法中。

Partial Class user_controls_myControl
    Public Sub bindData(ByVal commentList As List(Of CDTrialNotification))
       'attach to your gridviews here
    end sub
end Class

ASPX 页面:

   dim myList as new List(of String)
   me.myControl1.bindData(myList)

【讨论】:

  • 这只是解释如何绑定一个列表。我希望将提供给用户控件的列表解析为 html(我知道如何),但是对象的解析属性应该能够通过用户控件上的属性来决定。
猜你喜欢
  • 1970-01-01
  • 2017-02-05
  • 2021-09-03
  • 2015-04-20
  • 1970-01-01
  • 2013-02-10
  • 2014-09-06
  • 2019-05-05
  • 1970-01-01
相关资源
最近更新 更多