ComboBox的源代码(WebForm)

  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同一个文件夹中,属性的生成-》嵌入的资源。  

 

============================================================================

今天在网上看到的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值