通用 Asp.Net 表用户控件
我想设计一个表用户控件并动态添加自定义对象的属性。我想用具有泛型类型的集合对象填充此表。我怎样才能做到这一点?我无法将类的属性及其自定义标头发送到 PopulateTable。
到目前为止我所拥有的(在用户控件中)是..
<table class="datatable">
<thead>
<asp:Literal id="ltrlHeaders" runat="server" />
</thead>
<tbody>
<asp:Literal runat="server" ID="ltrlContent" />
</tbody>
</table>
public void PopulateTable(? myCollection)
{
List<string> headers = new List<string>();
FieldInfo[] fields = type.GetFields();
foreach (FieldInfo f in fields)
{
headers.Add(f.Name);
}
// headers
StringBuilder headerString = new StringBuilder();
headerString.Append("<thead>");
headers.ForEach(delegate(string h)
{
headerString.Append(String.Format("<th>{0}</th>", h));
});
headerString.Append("</thead>");
ltrlHeaders.Text = headerString.ToString();
StringBuilder contentString = new StringBuilder();
foreach (var item in list)
{
contentString.Append("<tr>");
foreach (string fieldInfo in headers)
{
contentString.Append(String.Format("<td>{0}</td>", type.GetField(fieldInfo).GetValue(item) as string));
}
contentString.Append("</tr>");
}
ltrlContent.Text = contentString.ToString();
}
I want to design a table user control and dynamically add the properties of custom objects. I want to populate this table with a collection object with a generic type. How can I do that ? I can not send the properties of the class to PopulateTable with its custom headers.
What I have so far (in a usercontrol) is..
<table class="datatable">
<thead>
<asp:Literal id="ltrlHeaders" runat="server" />
</thead>
<tbody>
<asp:Literal runat="server" ID="ltrlContent" />
</tbody>
</table>
public void PopulateTable(? myCollection)
{
List<string> headers = new List<string>();
FieldInfo[] fields = type.GetFields();
foreach (FieldInfo f in fields)
{
headers.Add(f.Name);
}
// headers
StringBuilder headerString = new StringBuilder();
headerString.Append("<thead>");
headers.ForEach(delegate(string h)
{
headerString.Append(String.Format("<th>{0}</th>", h));
});
headerString.Append("</thead>");
ltrlHeaders.Text = headerString.ToString();
StringBuilder contentString = new StringBuilder();
foreach (var item in list)
{
contentString.Append("<tr>");
foreach (string fieldInfo in headers)
{
contentString.Append(String.Format("<td>{0}</td>", type.GetField(fieldInfo).GetValue(item) as string));
}
contentString.Append("</tr>");
}
ltrlContent.Text = contentString.ToString();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设
T
对象可以转换为string
。或者您可能想要:如果您希望能够传递一个集合并了解其接口的一些信息。
Assuming that
T
objects can be cast tostring
. Or you might want:if you wanted to be able to pass a collection and know something about its interface.
一个非常简单的方法来做你想做的事情可能看起来像这样。我已经用我的代码中定义的人员列表更新了它。请注意,我还包含了一个从 Person 派生的类,以便您可以查看其他属性。
这有效地获取一个对象,使用反射来发现其属性,然后将它们写入表格单元格。您可以更改签名以接受您的自定义集合,并修改内部结构以呈现您的表。
A very simple method to do what you want might look like this. I've updated it with a List of Person as defined in my code. Note that I've also included a class derived from Person so you can see other properties.
This effectively takes an object, uses reflection to discover its properties, and then writes them out to table cells. You can change the signature to accept your custom collection, and modify the internals to render your table.