using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Lostinet.Sample
{
[
DefaultEvent("TextChanged"),
DefaultProperty("Text"),
]
public class ComboBox:WebControl,INamingContainer,IPostBackDataHandler
{
ListBox lb;
public ComboBox():
base(HtmlTextWriterTag.Input)
{
lb=new ListBox();
lb.Style["position"]="absolute";
lb.Style["display"]="none";
lb.Style["filter"]="progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')";
lb.EnableViewState=false;
Controls.Add(lb);
}
ListItemCollection _coll=new ListItemCollection();
[
PersistenceMode(PersistenceMode.InnerProperty),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
]
public ListItemCollection Items
{
get
{
return _coll;
}
}
[
DefaultValue(false),
]
public bool AutoPostBack
{
get
{
return ViewState["AutoPostBack"]==null?false:true;
}
set
{
if(value)
ViewState["AutoPostBack"]=0;
else
ViewState.Remove("AutoPostBack");
}
}
[
DefaultValue(""),
]
public string Text
{
get
{
return Convert.ToString(ViewState["Text"]);
}
set
{
ViewState["Text"]=value;
}
}
[
DefaultValue(0),
]
public int MaxLength
{
get
{
object o=ViewState["MaxLength"];
return o==null?0:(int)o;
}
set
{
ViewState["MaxLength"]=value;
}
}
static private string _scriptBlock;
static protected string ScriptBlock
{
get
{
if(_scriptBlock==null)
{
using(Stream s=typeof(ComboBox).Assembly.GetManifestResourceStream(typeof(ComboBox).FullName+".js"))
{
using(StreamReader sr=new StreamReader(s))
{
_scriptBlock="<script language=jscript>"+sr.ReadToEnd()+"</script>";
}
}
}
return _scriptBlock;
}
}
protected override void OnPreRender(System.EventArgs e)
{
base.OnPreRender(e);
Page.RegisterStartupScript(this.UniqueID,"<script>LostinetSampleComboBox_Init('"+this.UniqueID+"','"+lb.UniqueID+"');</script>");
lb.Items.Clear();
for(int i=0;i<Items.Count;i++)
lb.Items.Add(Items[i].Text);
string key=typeof(ComboBox).FullName;
if(!Page.IsClientScriptBlockRegistered(key))
{
Page.RegisterClientScriptBlock(key,ScriptBlock);
}
}
protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
writer.AddAttribute(HtmlTextWriterAttribute.Name,this.UniqueID);
writer.AddAttribute(HtmlTextWriterAttribute.Value,this.Text,true);
writer.AddAttribute("AutoComplete","Off");
if(MaxLength>0)
writer.AddAttribute(HtmlTextWriterAttribute.Maxlength,MaxLength.ToString(),false);
if(this.AutoPostBack)
writer.AddAttribute(HtmlTextWriterAttribute.Onchange,Page.GetPostBackEventReference(this),false);
}
public void RaisePostDataChangedEvent()
{
OnTextChanged(EventArgs.Empty);
}
public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{
string v=postCollection[postDataKey];
if(v==Text)
return false;
Text=v;
return true;
}
public event EventHandler TextChanged;
protected virtual void OnTextChanged(EventArgs e)
{
if(TextChanged!=null)
TextChanged(this,e);
}
protected override void LoadViewState(object savedState)
{
Pair p=(Pair)savedState;
base.LoadViewState(p.First);
((IStateManager)_coll).LoadViewState(p.Second);
}
protected override object SaveViewState()
{
return new Pair(base.SaveViewState(),((IStateManager)_coll).SaveViewState());
}
protected override void TrackViewState()
{
base.TrackViewState();
((IStateManager)_coll).TrackViewState();
}
}
}
=============================
脚本部分:ComboBox.js
function LostinetSampleComboBox_Init(inputID,listboxID)
{
var tb=document.all(inputID);
var lb=document.all(listboxID);
var isDisplaying=false;
for(var i=lb.options.length;i>0;i--)
lb.options[i]=new Option(lb.options[i-1].text,lb.options[i-1].value);
lb.options[0]=new Option(tb.value,tb.value);
var showlbTimerID=0;
var hidelbTimerID=0;
function ClearTimer()
{
if(hidelbTimerID!=0)
{
clearTimeout(hidelbTimerID);
hidelbTimerID=0;
}
}
function ShowLBSync()
{
var rect=tb.getBoundingClientRect();
lb.style.left=rect.left-document.body.clientLeft+"px";
lb.style.top=(rect.top-document.body.clientTop+tb.offsetHeight)+"px";
lb.style.width=rect.right-rect.left+"px";
lb.style.display="block";
isDisplaying=true;
ClearTimer();
}
function ShowLB()
{
if(showlbTimerID)
return;
ClearTimer();
showlbTimerID=setTimeout(
function(){
showlbTimerID=0;
ShowLBSync();
}
,10);
}
function HideLBSync()
{
lb.style.display="none";
isDisplaying=false;
ClearTimer();
}
function HideLB()
{
if(hidelbTimerID)
return;
ClearTimer();
hidelbTimerID=setTimeout(
function(){
hidelbTimerID=0;
HideLBSync();
}
,10);
}
function FocusToTextBox()
{
lb.selectedIndex=-1;
HideLBSync();
ClearTimer();
tb.focus();
}
tb.attachEvent("onclick",function(){
ShowLB();
});
tb.attachEvent("onblur",function(){
HideLB();
});
tb.attachEvent("onkeydown",function(){
if(event.keyCode==40)
{
document.selection.empty();
ShowLBSync();
lb.selectedIndex=0;
lb.focus();
ClearTimer();
return event.returnValue=!(event.cancelBubble=true);
}
});
lb.attachEvent("onfocus",function(){
ShowLB();
});
lb.attachEvent("onblur",function(){
HideLB();
});
lb.attachEvent("onclick",function(){
if(lb.selectedIndex>-1)
tb.value=lb[lb.selectedIndex].text;
FocusToTextBox();
});
lb.attachEvent("onkeydown",function(){
if(event.keyCode==38&&lb.selectedIndex==0)
{
FocusToTextBox();
}
if(
(
event.keyCode==13||event.keyCode==9
)
&&lb.selectedIndex>-1)
{
tb.value=lb[lb.selectedIndex].text;
FocusToTextBox();
return event.returnValue=!(event.cancelBubble=true);
}
if(event.keyCode==27)
{
FocusToTextBox();
return event.returnValue=!(event.cancelBubble=true);
}
});
}
==============
编译注意事项:
Namespace改为 工程默认名字空间[.目录[.目录...]]
把ComboBox.js放到和ComboBox同一个文件夹中,属性的生成-》嵌入的资源。
============================================================================
今天在网上看到的