加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 01:49:13 所属栏目:百科 来源:网络整理
导读:有没有办法定制asp.net下拉列表来支持自定义数据绑定字段.它不应该是控件的自定义属性.它应该能够通过DataBind()绑定数据;具有相同数据源的方法.在这个自定义服务器控件中,我正在尝试访问新的自定义字段,并且基于该值,我将对该数据源的特定行进行一些计算.
有没有办法定制asp.net下拉列表来支持自定义数据绑定字段.它不应该是控件的自定义属性.它应该能够通过DataBind()绑定数据;具有相同数据源的方法.在这个自定义服务器控件中,我正在尝试访问新的自定义字段,并且基于该值,我将对该数据源的特定行进行一些计算.

标准控制代码

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

新的自定义控件应该是这样的,

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

解决方法

你可以将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>具有一些属性,如Name,Department和ImageUrl.呈现下拉列表后如下所示:

<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向我发送了一个关于如何在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 
        }
    }

免责声明:这不是唯一的方法,但这是我能想到的最简单的方法,而不会覆盖LoadViewState和SaveViewState等.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读