可编辑下拉框,ComboBox asp.net控件

好久没更新了,发个有意思的,可编辑dropdownlist 控件,感觉自己写的挺牛的呵呵。跟大家共享下

代码
     public   class  ComboBox:System.Web.UI.WebControls.CompositeControl
    {

        
private  TextBox input;
        
private  DropDownList select;

        
public  TextBox TextBox {

            
get  {  this .EnsureChildControls();  return  input; }
        }

        
public  DropDownList DropDownList {

            
get  {  this .EnsureChildControls();  return  select; }
        }



        
protected   override   void  CreateChildControls()
        {
            
// base.CreateChildControls();

            
this .Controls.Clear();
            input 
=   new  TextBox();
            input.ID 
=   " input " ;

            select 
=   new  DropDownList();
            select.ID 
=    " select " ;

            
this .Controls.Add(select);
            
this .Controls.Add(input);
            
this .ChildControlsCreated  =   true ;
        }
        
protected   override   void  RenderContents(System.Web.UI.HtmlTextWriter writer)
        {
            
this .EnsureChildControls();

  
//            <select id="test1" style="width:100px;height:20px;">
  
// <option>中国</option>
  
// <option>aaaaa</option>
  
// <option>ccccc</option>
  
// <option>香港</option>
  
// </select>  
  
// <div style="width:80px; height:20px; position:absolute;" >
  
// <iframe frameborder="0" style="width:100%; height:100%;"></iframe>
  
// <input type="text" style="width:78px;height:17px;position:absolute; padding:0;" />
  
// </div>
            select.Width  =   100 ;
            select.Height 
=   20 ;
            select.RenderControl(writer);
            
// div
            writer.AddStyleAttribute( " width " " 80px " );
            writer.AddStyleAttribute(
" height " " 20px " );
            writer.AddStyleAttribute(
" position " " absolute " );
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            
// iframe
            writer.AddStyleAttribute( " width " " 100% " );
            writer.AddStyleAttribute(
" height " " 100% " );
            writer.AddAttribute(
" frameborder " " 0 " );
            writer.RenderBeginTag(HtmlTextWriterTag.Iframe);
            writer.RenderEndTag();
            
// input
            input.Width  =   78 ;
            input.Height 
=   15 ;
            input.Style.Add(HtmlTextWriterStyle.Position, 
" absolute " );
            input.RenderControl(writer);
            
// end div
            writer.RenderEndTag();

    
//             <script  type="text/javascript">
    
//     $(function () {
    
//         var select = $('#test1');
    
//         var offset = select.offset();
    
//         select.change(function () { $(this).next().find('input').val($(this).val()); })
    
//         .next().css({ left: offset.left, top: offset.top, zindex: 1000 })
    
//         .find('input').css({ left: 0, top: 0, zindex: 1001 });
    
//     })
    
// </script>
            writer.AddAttribute( " type " " text/javascript " );
            writer.RenderBeginTag(HtmlTextWriterTag.Script);
            writer.Write(
" $(function () {var select = $('# "   +   this .ID  +   " _ "   +   this .select.ID  +   " '); var offset = select.offset(); " );
            writer.Write(
" select.change(function () { $(this).next().find('input').val($(this).find(':selected').text()); }) " );
            writer.Write(
" .next().css({ left: offset.left, top: offset.top, zindex: 1000 }) " );
            writer.Write(
" .find('input').css({ left: 0, top: 0, zindex: 1001 });}) " );
            writer.RenderEndTag();
        
            


        }

    }

 实现方法比较简单,用的是组合控件。里面包着TextBox和DropDownList。技巧在于客户端。使用js和css将input正好定位到下拉框上面,遮住下拉框的显示区域,只留下下箭头区域。ie6下还得使用iframe来遮挡select.经测试在ie6先显示完美。ie8,获取显示有点小问题。仔细校对下可以修改过了,主要原因在于不同浏览器select下拉框的下箭头区域大小不一致。

 

 

使用时候也比较简单,直接操作控件的DropDownList属性和TextBox属性即可。

 

 

代码
            ComboBox1.DropDownList.DataSource  =   new  List < object > () {  new  { Text  =   " 111 " , value  =   1  },  new  { Text  =   " 222 " , value  =   2  } };
            ComboBox1.DropDownList.DataTextField 
=   " Text " ;
            ComboBox1.DropDownList.DataValueField 
=   " Value " ;
            ComboBox1.DropDownList.DataBind();


 

转载于:https://www.cnblogs.com/xhan/archive/2010/10/16/1853321.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值