我自己写一个类似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>
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>
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();
}
}
}
}
}
<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就是新添加的属性
private string _value;
private string _object;既然 value 和object 都是string,那么请问他们有什么区别呢?为什么不能用一个值?
就好像dropdownlist是html控件么?是asp.net的一个对象,是一种数据类型,即使不在页面上show,后台就可以把他当成名值对数组使用