如何自定义asp.net DropDownList以支持自定义数据绑定字段

时间:2011-11-19 20:06:22

标签: c# asp.net

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

标准控制代码

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

新的自定义控件应如下所示,

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

1 个答案:

答案 0 :(得分:7)

您可以将DropDownList扩展为以下内容:

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>一些属性,例如NameDepartmentImageUrl。呈现下拉列表后如下所示:

<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>

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

的问题

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++;
                }
            }
        }
    }

您可以访问CustomProperty上的SelectedIndexChanged,因为:

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等。