通用 Asp.Net 表用户控件

发布于 2024-12-19 19:14:57 字数 1501 浏览 1 评论 0原文

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

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

发布评论

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

评论(2

り繁华旳梦境 2024-12-26 19:14:57
public void PopulateTable<T>(T myCollection)

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

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

如果您希望能够传递一个集合并了解其接口的一些信息。

public void PopulateTable<T>(T myCollection)

Assuming that T objects can be cast to string. Or you might want:

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

if you wanted to be able to pass a collection and know something about its interface.

浅笑依然 2024-12-26 19:14:57

一个非常简单的方法来做你想做的事情可能看起来像这样。我已经用我的代码中定义的人员列表更新了它。请注意,我还包含了一个从 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;

    }


}

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

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.

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;

    }


}

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.

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