如何自定义asp.net下拉列表以支持自定义数据绑定字段

本文关键字:自定义 支持 数据绑定 字段 net asp 下拉列表 | 更新日期: 2023-09-27 18:15:08

是否有办法自定义asp.net下拉列表来支持自定义数据绑定字段。它不应该是控件的自定义属性。它应该能够通过DataBind();方法与相同的数据源绑定数据。在这个自定义服务器控件中,我试图访问新的自定义字段,并基于该值,我将为数据源的特定行做一些计算。

标准控制代码

<asp:DropDownList runat="server" DataTextField="TextField" DataValueField="ValueField"/> 

新的自定义控件应该看起来像这样,

<asp:DropDownList runat="server" DataTextField="TextField" DataValueField="ValueField" DataCustomField="CustomField"/>

如何自定义asp.net下拉列表以支持自定义数据绑定字段

你可以这样扩展下拉列表:

public class MyDropDownList : DropDownList
{
    public MyDropDownList()
    {   
    }
    public string CustomProperty { get; set; }
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        int i = 0;
        foreach (var item in this.DataSource as IEnumerable)
        {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(item);
            PropertyDescriptor pd = properties.Find(CustomProperty, true);
            this.Items[i].Attributes.Add(CustomProperty, pd.GetValue(item).ToString());
            i++;
        }          
    }
}

并在你的标记上使用它,如下所示:

<cc1:MyDropDownList ID="MyDropDownList1" DataTextField="Name" DataValueField="Department" CustomProperty="ImageUrl" runat="server">
</cc1:MyDropDownList>

在我的情况下,我绑定到一个List<Employee>与一些属性,如Name, DepartmentImageUrl。渲染后的下拉菜单如下所示:

<select name="ctl00$MainContent$MyDropDownList1" id="MainContent_MyDropDownList1">
    <option selected="selected" value="Human resources" ImageUrl="http://www.freedigitalphotos.net/images/gal_images/av-_146.jpg">Employee 0</option>
    <option value="Information Technology" ImageUrl="http://www.freedigitalphotos.net/images/gal_images/av-_314.jpg">Employee 1</option>
    <option value="Human resources" ImageUrl="http://www.freedigitalphotos.net/images/gal_images/av-_146.jpg">Employee 2</option>
    <option value="Information Technology" ImageUrl="http://www.freedigitalphotos.net/images/gal_images/av-_314.jpg">Employee 3</option>
    <option value="Human resources" ImageUrl="http://www.freedigitalphotos.net/images/gal_images/av-_146.jpg">Employee 4</option>
</select>

UPDATE:(对于@emrahozguner,他通过Twitter向我发送了一个关于如何在SelectedIndexChanged事件上检索CustomProperty的问题)

public class MyDropDownList : DropDownList
    {
        public MyDropDownList()
        {
        }
        public string CustomProperty
        {
            get;
            set;
        }
        public string SelectedCustomProperty
        {
            get
            {
                //Use the SelectedIndex to retrieve the right element from ViewState
                return ViewState["CustomProperty" + this.SelectedIndex] as string;
            }
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            int i = 0;
            if (this.DataSource != null)
            {
                foreach (var item in this.DataSource as IEnumerable)
                {
                    PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(item);
                    PropertyDescriptor pd = properties.Find(CustomProperty, true);
                    this.Items[i].Attributes.Add(CustomProperty, pd.GetValue(item).ToString());
                    //We need to save the CustomProperty value on ViewState if we want to be able to retrieve it...
                    ViewState["CustomProperty" + i] = pd.GetValue(item).ToString();
                    i++;
                }
            }
        }
    }

SelectedIndexChanged上访问CustomProperty:

protected void MyDropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        MyDropDownList l = (sender as MyDropDownList);
        if (l != null)
        {
            string selectedCustomProperty = l.SelectedCustomProperty;
            //Do something cool with this selectedCustomProperty 
        }
    }

免责声明:这不是唯一的方法,但这是我能想到的最简单的方法,不需要重写LoadViewStateSaveViewState