我自己写一个类似dropdownlist的控件,但是不是下拉框,数据存储类似
dropdownlist是名值对的,即:(text,value)
我现在需要的是(text,value,object),存3个值后台实现是简单的,继承一下CollectionBase,写个add属性就好。Items就会有数据
但是这个控件如何实现在前台赋值
比如dropdownlist可以在前台这样赋值
<asp:dropdownlist id="Ddl_Kind" runat="server">
<asp:ListItem Value="">———请选择———</asp:ListItem>
<asp:ListItem Value="101">这个是在UI添加下拉框元素</asp:ListItem>
         <asp:ListItem Value="102">我要的是在UI添加我新写的控件的元素</asp:ListItem>
</asp:dropdownlist>

解决方案 »

  1.   

    定义一个类用声明数据项
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;namespace CustomComponents
    {
        public class ListItem
        {
            private string _text;
            private string _value;
            private string _object;        [Category("Behavior")]
            [DefaultValue("")]
            [Description("项文本")]
            [NotifyParentProperty(true)]
            public string Text
            {
                get { return _text; }
                set { _text = value; }
            }        [Category("Behavior")]
            [DefaultValue("")]
            [Description("项值")]
            [NotifyParentProperty(true)]
            public string Value
            {
                get { return _value; }
                set { _value = value; }
            }        [Category("Behavior")]
            [DefaultValue("")]
            [Description("项对象")]
            [NotifyParentProperty(true)]
            public string Ojbect
            {
                get { return _object; }
                set { _object = value; }
            }
        }
    }定义一个集合项类
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;namespace CustomComponents
    {
        public class Items:List<ListItem>
        {
            public Items()
                : base()
            {
            }    }
    }定义控件
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;namespace CustomComponents
    {
        [ToolboxData("<{0}:DropDownList runat=server></{0}:DropDownList>")]
        [PersistChildren(false)]
        [ParseChildren(true,"Items")]
        public class DropDownList : WebControl
        {
            private Items items;        [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
            [TypeConverter(typeof(CollectionConverter))]
            [Category("杂项")]
            public Items Items
            {
                get
                {
                    if (this.items == null)
                        items = new Items();
                    return items;
                }        }        protected override HtmlTextWriterTag TagKey
            {
                get
                {
                    return HtmlTextWriterTag.Select;
                }
            }        protected override void RenderContents(HtmlTextWriter writer)
            {
                if (items == null)
                    items = new Items();
                foreach (ListItem item in items)
                {
                    if (items.Count > 0)
                    {
                        writer.AddAttribute(HtmlTextWriterAttribute.Value, item.Value);
                        writer.RenderBeginTag(HtmlTextWriterTag.Option);
                        writer.Write(item.Text);
                        writer.RenderEndTag();
                    }
                }
            }
        }
    }
      

  2.   

    LZ的方法是一种,不过下面的方法也可以实现啊,而且比较简单。1. 在页面上加一个服务器端的HTML下拉列表控件
    <select runat="server" id="slt">
       <option>aaa</option>
    </select>2. 后台为下拉列表绑值,对于VALUE和TEXT以外的绑定值,可以通过方法addAttribute方法增加一个属性来实现。
    就像下面的HTML代码:
    <select runat="server" id="slt">
       <option value="aaa" value1="bbb">aaa</option>
    </select>
    其中value1就是新添加的属性
      

  3.   

    在web里面怎么会有什么“底层控件”?怎么写都是生产HTML,交给浏览器解析。要不就是js、ajax控制dom。你说的底层控件是什么意思呢?c# c++都是工作在服务器端的,难道你想写OCX控件?=========
    private string _value;
    private string _object;既然 value 和object 都是string,那么请问他们有什么区别呢?为什么不能用一个值?
      

  4.   

    不是html控件,是asp.net控件,com组件,或者说是一个类,也是一个对象
    就好像dropdownlist是html控件么?是asp.net的一个对象,是一种数据类型,即使不在页面上show,后台就可以把他当成名值对数组使用