2

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

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

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

物体:

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

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

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

或在后面的代码中设置:

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

1 回答 1

0

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

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

用户控制:

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)
于 2013-03-04T20:08:39.917 回答