通用Asp.Net表UserControl

时间:2011-12-05 23:20:07

标签: asp.net generics reflection

我想设计一个表用户控件并动态添加自定义对象的属性。我想用一个泛型类型的集合对象填充此表。我怎样才能做到这一点 ?我无法使用自定义标头将类的属性发送到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();
    }

2 个答案:

答案 0 :(得分:0)

public void PopulateTable<T>(T myCollection)

假设T个对象可以转换为string。或者你可能想要:

public void PopulateTable<T>(IEnumerable<T> myCollection)

如果您希望能够传递集合并了解其界面。

答案 1 :(得分:0)

一个非常简单的方法来做你想做的事情可能看起来像这样。我用我的代码中定义的Person列表更新了它。请注意,我还包含了一个派生自Person的类,因此您可以看到其他属性。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;

public partial class _Default : System.Web.UI.Page
{

    public class Person
    {
        public string Name { get; set; }

        public string Title { get; set; }

    }


    public class WorkingPerson : Person
    {

        public string Job { get; set; }

    }


    protected void Page_Load(object sender, EventArgs e)
    {

        List<Person> people = new List<Person>();

        people.Add(new Person() {Name = "T", Title = "Mr" }) ;
        people.Add(new WorkingPerson() {Name="Face", Title="Mr", Job ="Film Star" } );


        TextBox textBox = new TextBox();

        Controls.Add(CreateTableFromObject<Person>(people));

    }



    public PlaceHolder CreateTableFromObject<T>(IEnumerable<T> objectList) where T : Person
    {

        PlaceHolder holder = new PlaceHolder(); 

        foreach (T thing in objectList)
        {

            Table table = new Table();


            PropertyInfo[] properties = thing.GetType().GetProperties();

            TableRow propertiesSpan = new TableRow();
            propertiesSpan.Cells.Add(new TableCell() { ColumnSpan = properties.Length, Text = String.Format("Properties of {0}", thing.GetType().FullName) });

            table.Rows.Add(propertiesSpan);

            TableRow tableRow = new TableRow();


            foreach (PropertyInfo propertyInfo in properties)
            {
                TableCell tc = new TableCell();
                tc.Text = propertyInfo.Name;
                tableRow.Cells.Add(tc);
            }

            table.Rows.Add(tableRow);

            holder.Controls.Add(table);

        }

        return holder;

    }


}

这有效地获取一个对象,使用反射来发现它的属性,然后将它们写出到表格单元格。您可以更改签名以接受自定义集合,并修改内部以呈现表格。