可輸入的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';
    }

}
發佈了45 篇原創文章 · 獲贊 29 · 訪問量 22萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章