使用DisplayNameAttribute在ASP.NET [英] Use DisplayNameAttribute in ASP.NET

查看:91
本文介绍了使用DisplayNameAttribute在ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个列表绑定到一个网页上的GridView,但覆盖方式的属性名称注释通过显示。我以为System.ComponentModel会的工作,但这似乎并没有工作。难道这只是针对Windows窗体:

I want to bind a List to a GridView on a web page, but override the way the property names display via annotation. I thought System.ComponentModel would work, but this doesn't seem to work. Is this only meant for Windows Forms?:

using System.ComponentModel;

namespace MyWebApp
{
    public class MyCustomClass
    {
        [DisplayName("My Column")]
        public string MyFirstProperty
        {
            get { return "value"; }
        }

    public MyCustomClass() {}
}

然后在页面上:

protected void Page_Load(object sender, EventArgs e)
{
    IList<MyCustomClass> myCustomClasses = new List<MyCustomClass>
    {
        new MyCustomClass(),
        new MyCustomClass()
    };

TestGrid.DataSource = myCustomClasses;
TestGrid.DataBind();

}

这使得以MyFirstProperty作为列标题,而不是我的专栏。这难道不应该工作?

This renders with "MyFirstProperty" as the column header rather than "My Column." Isn't this supposed to work?

推荐答案

SirDemon说什么......

What SirDemon said...

答案似乎是否定的,你不能。至少不会开箱。

The answer appears to be no, you can't. At least not out of the box.

该System.Web.UI.WebControls.GridView使用反映属性的名称:

The System.Web.UI.WebControls.GridView uses reflected property's name:

protected virtual AutoGeneratedField CreateAutoGeneratedColumn(AutoGeneratedFieldProperties fieldProperties)
{
    AutoGeneratedField field = new AutoGeneratedField(fieldProperties.DataField);
    string name = fieldProperties.Name; //the name comes from a PropertyDescriptor
    ((IStateManager) field).TrackViewState();
    field.HeaderText = name; //<- here's reflected property name
    field.SortExpression = name;
    field.ReadOnly = fieldProperties.IsReadOnly;
    field.DataType = fieldProperties.Type;
    return field;
}

虽然System.Windows.Forms.DataGridView使用显示名称(如果可用):

While System.Windows.Forms.DataGridView uses DisplayName if available:

public DataGridViewColumn[] GetCollectionOfBoundDataGridViewColumns()
{
	...
	ArrayList list = new ArrayList();
	//props is a collection of PropertyDescriptors
	for (int i = 0; i < this.props.Count; i++)
	{
		if (...)
		{
			DataGridViewColumn dataGridViewColumnFromType = GetDataGridViewColumnFromType(this.props[i].PropertyType);
			...
			dataGridViewColumnFromType.Name = this.props[i].Name;
			dataGridViewColumnFromType.HeaderText = !string.IsNullOrEmpty(this.props[i].DisplayName) ? this.props[i].DisplayName : this.props[i].Name;
		}
	}
	DataGridViewColumn[] array = new DataGridViewColumn[list.Count];
	list.CopyTo(array);
	return array;
}

不幸的是,当你可以重写CreateAutoGeneratedColumn,既不缺少的显示名称,也没有基本的属性描述符被传递,并不能覆盖CreateAutoGeneratedColumns(虽然你可以CreateColumns)。

Unfortunately, while you can override the CreateAutoGeneratedColumn, neither the missing DisplayName nor underlying property descriptor gets passed, and you can't override CreateAutoGeneratedColumns (although you could CreateColumns).

这意味着你必须自己遍历反映属性和其他一些地方。

This means you'd have to iterate over reflected properties yourself and in some other place.

这篇关于使用DisplayNameAttribute在ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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