可输入的Web下拉列表框控件

vs2005提供的web下拉列表框 不支持输入,只能选择,我们可以继承该控件来支持输入功能,实现的原理很简单,就是增加一个textbox控件叠加在下拉列表框上,客户端通过脚本来同步这两个控件的值,先看看服务端代码:

 

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Collections;
using  System.Web.UI.WebControls;
using  System.ComponentModel;
using  System.Text.RegularExpressions;
using  System.Drawing;
using  System.Web.UI;

namespace  Weiky.Web.Control
{
    
public class WeikyWebDropDownList : DropDownList
    
{
        
private TextBox _textBox;
        
private Label _label;

        
public WeikyWebDropDownList()
        
{
            _textBox 
= new TextBox();
            _label 
= new Label();
            Caption 
= "标签";
            SetFont(
9"宋体");
            
if (this.Parent != null)
            
{
                
this.Parent.Controls.Add(_textBox);
            }

        }


        
客户端事件

        
public void SetFont(int size, string name)
        
{
            _label.Font.Name 
= name;
            _label.Font.Size 
= size;
        }


        
公共属性

        
protected override void Render(System.Web.UI.HtmlTextWriter output)
        
{
            output.WriteBeginTag(
"Table");
            output.WriteAttribute(
"style"string.Format("width:{0}px;z-index:1;position:absolute;Left:{1}px;Top:{2}px", Width.Value, Left,Top));
            output.WriteAttribute(
"cellspacing""0");
            output.WriteAttribute(
"cellpadding""0");
            output.Write(HtmlTextWriter.TagRightChar);
            output.WriteBeginTag(
"Tr");
            output.WriteAttribute(
"Nowrap""true");
            output.Write(HtmlTextWriter.TagRightChar);
            output.WriteBeginTag(
"Td");
            output.WriteAttribute(
"valign""baseline");
            output.WriteAttribute(
"align""left");
            output.WriteAttribute(
"width", ((1 - TextwidthScale) * 100).ToString() + "%");
            output.Write(HtmlTextWriter.TagRightChar);
            _label.RenderControl(output);
            output.WriteEndTag(
"Td");
            output.WriteBeginTag(
"Td");
            output.WriteAttribute(
"width", (TextwidthScale * 100).ToString() + "%");
            output.Write(HtmlTextWriter.TagRightChar);

            
if (Change != "")
            
{
                Attributes.Add(
"onchange", Editable ? "DropDownList_OnChange('" + ClientID + "',this);" + Change : Change);
            }

            
else if(Editable)
            
{
                Attributes.Add(
"onchange""DropDownList_OnChange('" + ClientID + "',this)");
            }


            
base.Render(output);

            
if (Editable)
            
{
                _textBox.ID 
= ClientID + "_inputControl";
                _textBox.Style.Clear();
                _textBox.Style.Add(
"POSITION""absolute");
                _textBox.Style.Add(
"MARGIN-LEFT""-" + base.Width);
                _textBox.Style.Add(
"width", _textBox.Width.ToString());
                
                
if (Blur != "")
                
{
                    _textBox.Attributes.Add(
"onblur""DropDownList_OnInputBlur('" + ClientID + "',this);" + Blur);
                }

                
else
                
{
                    _textBox.Attributes.Add(
"onblur""DropDownList_OnInputBlur('" + ClientID + "',this)");
                }

                
if (KeyDown != "")
                
{
                    _textBox.Attributes.Add(
"onkeydown", KeyDown);
                }

                
if (KeyUp != "")
                
{
                    _textBox.Attributes.Add(
"onkeyup", KeyUp);
                }

                
if (KeyPress != "")
                
{
                    _textBox.Attributes.Add(
"onkeypress", KeyPress);
                }

                
if (Focus != "")
                
{
                    _textBox.Attributes.Add(
"onfocus", Focus);
                }

                _textBox.RenderControl(output);
            }

            output.WriteEndTag(
"Td");
            output.WriteEndTag(
"Tr");
            output.WriteEndTag(
"Table"); 
        }


        
public void SetTextBoxValue(string value)
        
{
            
if (_textBox != null)
            
{
                _textBox.Text 
= value;
            }

        }


    }

}

 

这个控件还包括前缀标签,说明该控件的名称,组合成一个完整的控件。

客户端通过两个脚本函数同步输入框控件和下拉框控件的值:

 

function  DropDownList_OnChange(id,theControl)
{
    document.all(theControl.id 
+ '_inputControl').value = DropDownList_GetText(theControl);
}


function  DropDownList_OnInputBlur(id,theInput)
{
    
var theControl = GetControlByName('8',id);
    
var isRela = theControl.attributes.getNamedItem('isRela');
    
if(isRela) return;
    
var valueIsValid = false;
    
for(var i=0;i<theControl.children.length;i++)
    
{
        
if(theInput.value == theControl.children[i].value || theControl.children[i].innerText.indexOf(theInput.value) > -1)
        
{
            theControl.children[i].selected 
= true;
            theInput.value 
= theControl.children[i].innerText;
            valueIsValid 
= true;
            
break;
        }

    }

    
if(!valueIsValid)
    
{
        theInput.style.color 
= 'red';
    }

    
else
    
{
        theInput.style.color 
= 'black';
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值