是否可以从数据绑定项检索属性名称? [英] Is it possible to retrieve property names from a databound item?

查看:130
本文介绍了是否可以从数据绑定项检索属性名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码隐藏在数据库中检查中继器中的每个项目,以查看作者/日期是否为空(没有作者/日期,或者系统设置为不显示它们),以便我可以清除各自的标签。这是我没有得到像发布者当没有作者和/或指定的日期。

I have this code-behind that checks each item in a repeater when it is databound to see if the author/date are empty (either no author/date, or the system is set to not display them) so that way I can clear out their respective labels. This is so I dont get something like "Posted By on" when there is not author and/or date specified.

这是代码:

protected void Page_Load(object sender, EventArgs e)
{
    repeater.ItemDataBound += new RepeaterItemEventHandler(repeater_ItemDataBound);    
}

void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Literal PostedBy = (Literal)e.Item.FindControl("litPostedBy");
        Literal PostedOn = (Literal)e.Item.FindControl("litPostedOn");
        string Author = (string)DataBinder.Eval(e.Item.DataItem, "Author");
        string Date = (string)DataBinder.Eval(e.Item.DataItem, "PubDate");

        if (string.IsNullOrEmpty(Author))
        {
            if (string.IsNullOrEmpty(Date))
            {
                PostedBy.Text = "";
                PostedOn.Text = "";
            }
            else
            {
                PostedBy.Text = "Posted ";
            }

        }
    }
}

我正在使用CMS,我不确定在 e.Item.DataItem 中的所有属性。有没有办法循环遍历DataItem并打印出属性名称/值?

I am using a CMS, and I am unsure of what all the properties are in the e.Item.DataItem. Is there some way I can loop through the DataItem and print out the property names/values?

谢谢!

推荐答案

什么属性DataItem将取决于它包含什么样的对象。当数据绑定中继器时,它将包含当前正在处理的数据源中的对象。以下方法接受任何对象并列出其所包含的属性:

What properties DataItem will have depends on what kind of object it contains. It will contain the object from the data source that is currently being processed when databinding the repeater. The following method takes any object and lists the properties it contains:

private static void PrintAllProperties(object obj)
{
    obj.GetType().
        GetProperties().
        ToList().
        ForEach(p => 
            Console.WriteLine("{0} [{1}]", p.Name, p.PropertyType.ToString()
            ));
}

示例输出(对于String实例):

Example output (for a String instance):

Chars [System.Char]
Length [System.Int32]

这篇关于是否可以从数据绑定项检索属性名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆