国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

ASP.NET: Custom AutoCompleteTextBox WebControl [With Source Code]

2019-11-18 17:26:06
字體:
供稿:網(wǎng)友

這是一個Teddy最近封裝的AutoCompleteTextBox。我們知道,asp.net本身的TextBox也是支持一定的AutoComplete功能的,但是那是依賴瀏覽器實現(xiàn)的,并不能指定自定義的AutoComplete候選項。本文列舉的AutoCompleteTextBox則彌補(bǔ)了這個缺憾。只需設(shè)置AutoCompleteTextBox.AutoCompleteData屬性,傳遞一個string[],就能使TextBox支持自定義候選項了。

AutoComplete邏輯

如果沒有匹配當(dāng)前輸入的候選項,則同一般的TextBox;
如果只有一個候選項與當(dāng)前輸入匹配,則自動完成;
如果有超過一個候選項與當(dāng)前輸入匹配,則在textbox中自動完成第一個候選項,并彈出包含所有候選項的彈出框。

實現(xiàn)源碼

源碼是在VS2005編譯的,不過實際上幾乎沒有使用依賴2.0的語法,在vs2003下經(jīng)極少修改就同樣能編譯的。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Ilungasoft.Framework.Web.UI.WebControls
{
    [ToolboxData("<{0}:AutoCompleteTextBox runat=server></{0}:AutoCompleteTextBox>")]
    public class AutoCompleteTextBox : WebControl
    {
        PRivate Members#region Private Members

        private TextBox textBox = new TextBox();
        private HtmlGenericControl autoCompleteFrame = new HtmlGenericControl();

        private string ToJsStringArray(params string[] strs)
        {
            if (strs != null && strs.Length > 0)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(" new Array(");

                foreach (string str in strs)
                {
                    sb.Append(string.Format("'{0}', ", str.Replace("'", "//'")));
                }

                return sb.ToString().TrimEnd(',', ' ') + ");";
            }
            else
            {
                return " new Array;";
            }
        }

        private string MakeUniqueID(string id)
        {
            if (id != null && id.Trim().Length > 0)
            {
                return string.Concat(this.UniqueID.Replace("$", "_"), "_", id);
            }
            else
            {
                return this.UniqueID.Replace("$", "_");
            }
        }

        #endregion

        Properties#region Properties

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                if (Page.IsPostBack)
                {
                    textBox.Text = Page.Request.Form[MakeUniqueID("textBox")];
                }
                return textBox.Text;
            }
            set
            {
                textBox.Text = value;
            }
        }

        [Bindable(true)]
        [Category("Behavior")]
        [DefaultValue("")]
        [Localizable(true)]
        public int MaxLength
        {
            get
            {
                return textBox.MaxLength;
            }
            set
            {
                textBox.MaxLength = value;
            }
        }

        [Bindable(true)]
        [Category("Behavior")]
        [DefaultValue(false)]
        [Localizable(true)]
        public bool ReadOnly
        {
            get
            {
                return textBox.ReadOnly;
            }
            set
            {
                textBox.ReadOnly = value;
            }
        }

        public string[] AutoCompleteData
        {
            get
            {
                string[] s = (string[])ViewState["AutoCompleteData"];
                return ((s == null) ? null : s);
            }
            set
            {
                ViewState["AutoCompleteData"] = value;
            }
        }

        #endregion

        Overriden Members#region Overriden Members

        protected override void CreateChildControls()
        {
            create textBox#region create textBox

            textBox.ID = MakeUniqueID("textBox");
            textBox.AutoCompleteType = AutoCompleteType.Disabled;
            textBox.Attributes.Add("onkeypress", string.Format("return __DoAutoComplete(event, '{0}')", MakeUniqueID(null)));
            textBox.Attributes.Add("onblur", string.Format("if (!document.show_{0}) document.getElementById('{1}').style.display = 'none';", MakeUniqueID(null), MakeUniqueID("autoCompleteFrame")));
            textBox.Width = Width;

            #endregion

            create autoCompleteFrame#region create autoCompleteFrame

            autoCompleteFrame.TagName = "iframe";
            autoCompleteFrame.ID = MakeUniqueID("autoCompleteFrame");
            autoCompleteFrame.Attributes.Add("style", "display:none; position: absolute; border: ridge 1px");
            autoCompleteFrame.Attributes.Add("frameborder", "0");
            autoCompleteFrame.Attributes.Add("marginheight", "0");
            autoCompleteFrame.Attributes.Add("marginwidth", "2");
            autoCompleteFrame.Attributes.Add("scrolling", "auto");
            autoCompleteFrame.Attributes.Add("width", Width.ToString());
            autoCompleteFrame.Attributes.Add("height", "100px");
            autoCompleteFrame.Attributes.Add("src", "javascr            autoCompleteFrame.Attributes.Add("onmouSEOver", string.Format("document.show_{0} = true;", MakeUniqueID(null)));
            autoCompleteFrame.Attributes.Add("onmouseout", string.Format("document.show_{0} = false;", MakeUniqueID(null)));

            #endregion
        }

        protected override void OnPreRender(EventArgs e)
        {
            Register Client Script Block#region Register Client Script Block

            if (!Page.ClientScript.IsClientScriptBlockRegistered("__DoAutoComplete"))
            {
                string script = string.Concat(
                    "<script language=/"Javascript/" type=/"text/javascript/">/r/n",
                    "    var isOpera = navigator.userAgent.indexOf('Opera') > -1;/r/n",
                    "    var isIE = navigator.userAgent.indexOf('MSIE') > 1 && !isOpera;/r/n",
                    "    var isMoz = navigator.userAgent.indexOf('Mozilla/5.') == 0 && !isOpera;/r/n",
                    "/r/n",
                    "    function textboxSelect (oTextbox, iStart, iEnd)/r/n",
                    "    {/r/n",
                    "       switch(arguments.length) {/r/n",
                    "           case 1:/r/n",
                    "               oTextbox.select();/r/n",
                    "               break;/r/n",
                    "/r/n",
                    "           case 2:/r/n",
                    "               iEnd = oTextbox.value.length;/r/n",
                    "               /* falls through *//r/n",
                    "               /r/n",
                    "           case 3:          /r/n",
                    "               if (isIE) {/r/n",
                    "                   var oRange = oTextbox.createTextRange();/r/n",
                    "                   oRange.moveStart(/"character/", iStart);/r/n",
                    "                   oRange.moveEnd(/"character/", -oTextbox.value.length + iEnd);      /r/n",
                    "                   oRange.select();                                              /r/n",
                    "               } else if (isMoz){/r/n",
                    "                   oTextbox.setSelectionRange(iStart, iEnd);/r/n",
                    "               }                    /r/n",
                    "       }/r/n",
                    "/r/n",
                    "       oTextbox.focus();/r/n",
                    "    }/r/n",
                    "/r/n",
                    "    function textboxReplaceSelect (oTextbox, sText)/r/n",
                    "    {/r/n",
                    "       if (isIE) {/r/n",
                    "           var oRange = document.selection.createRange();/r/n",
                    "           oRange.text = sText;/r/n",
                    "           oRange.collapse(true);/r/n",
                    "           oRange.select();                                /r/n",
                    "       } else if (isMoz) {/r/n",
                    "           var iStart = oTextbox.selectionStart;/r/n",
                    "           oTextbox.value = oTextbox.value.substring(0, iStart) + sText + oTextbox.value.substring(oTextbox.selectionEnd, oTextbox.value.length);/r/n",
                    "           oTextbox.setSelectionRange(iStart + sText.length, iStart + sText.length);/r/n",
                    "       }/r/n",
                    "/r/n",
                    "       oTextbox.focus();/r/n",
                    "    }/r/n",
                    "/r/n",
                    "    function autocompleteMatch (sText, arrValues)/r/n",
                    "    {/r/n",
                    "       var retMatches = /"/"; /r/n",
                    "       /r/n",
                    "       for (var i=0; i < arrValues.length; i++)/r/n",
                    "       {/r/n",
                    "           if (arrValues[i].indexOf(sText) == 0)/r/n",
                    "           {/r/n",
                    "               retMatches += arrValues[i] + ',';/r/n",
                    "           }/r/n",
                    "       }/r/n",
                    "      /r/n",
                    "      if (retMatches.length > 0)/r/n",
                    "      {/r/n",
                    "          retMatches = retMatches.substr(0, retMatches.length - 1);/r/n",
                    "      } /r/n",
                    "/r/n",
                    "       return retMatches;/r/n",
                    "    }/r/n",
                    "/r/n",
                    "    function __DoAutoComplete(oEvent, id)/r/n",
                    "    {/r/n",
                    "        var oTextbox = document.getElementById(id + '_textBox');/r/n",
                    "        var frame = document.getElementById(id + '_autoCompleteFrame');/r/n",
                    "        var arrValues =  document[id + '_data'];/r/n",
                    "       /r/n",
                    "        switch (oEvent.keyCode) /r/n",
                    "        {/r/n",
                    "            case 38: //up arrow  /r/n",
                    "            case 40: //down arrow/r/n",
                    "            case 37: //left arrow/r/n",
                    "            case 39: //right arrow/r/n",
                    "            case 33: //page up  /r/n",
                    "            case 34: //page down  /r/n",
                    "            case 36: //home  /r/n",
                    "            case 35: //end                  /r/n",
                    "            case 13: //enter  /r/n",
                    "            case 9: //tab  /r/n",
                    "            case 27: //esc  /r/n",
                    "            case 16: //shift  /r/n",
                    "            case 17: //ctrl  /r/n",
                    "            case 18: //alt  /r/n",
                    "            case 20: //caps lock/r/n",
                    "            case 8: //backspace  /r/n",
                    "            case 46: //delete/r/n",
                    "                return true;/r/n",
                    "                break;/r/n",
                    "                /r/n",
                    "            default:/r/n",
                    "                textboxReplaceSelect(oTextbox, String.fromCharCode(isIE ? oEvent.keyCode : oEvent.charCode));/r/n",
                    "                var iLen = oTextbox.value.length;/r/n",
                    "/r/n",
                    "                var sMatches = autocompleteMatch(oTextbox.value, arrValues);/r/n",
                    "/r/n",
                    "                if (sMatches.length > 0)/r/n",
                    "                {/r/n",
                    "                   var arrMatches = sMatches.split(',');/r/n",
                    "                   oTextbox.value = arrMatches[0];/r/n",
                    "                   textboxSelect(oTextbox, iLen, oTextbox.value.length);/r/n",
                    "                  /r/n",
                    "                   if (arrMatches.length > 1)/r/n",
                    "                   {/r/n",
                    "                        frame.style.display = 'inline';/r/n",
                    "                        frame.height = '100';/r/n",
                    "                        /r/n",
                    "                        frame.contentWindow.document.body.innerHTML = '';/r/n",
                    "                        for (var i = 0; i < arrMatches.length; i++)/r/n",
                    "                        {/r/n",
                    "                            frame.contentWindow.document.body.innerHTML += '<div style=/"width: 100%; cursor: default/" onmouseover=/"this.style.backgroundColor=//'#316AC5//'; this.style.color=//'white//';/" onmouseout=/"this.style.backgroundColor=//'//'; this.style.color=//'black//';/" onclick=/"parent.document.getElementById(//'' + id + '_textBox//').value = this.innerHTML/">' + arrMatches[i] + '</div>';/r/n",
                    "                        }/r/n",
                    "                        /r/n",
                    "                        frame.contentWindow.document.body.style.backgroundColor = 'white';/r/n",
                    "                        frame.contentWindow.document.onclick = function() { document.getElementById(id + '_autoCompleteFrame').style.display = 'none'; };/r/n",
                    "                   }  /r/n",
                    "                }  /r/n",
                    "                /r/n",
                    "                return false;/r/n",
                    "        }        /r/n",
                    "    }/r/n",
                    "</script>/r/n",
                    "");
                Page.ClientScript.RegisterClientScriptBlock(typeof(string), "__DoAutoComplete", script);
            }

            if (!Page.ClientScript.IsClientScriptBlockRegistered(MakeUniqueID("data")))
            {
                Page.ClientScript.RegisterClientScriptBlock(typeof(string), MakeUniqueID("data"), string.Format("<script language=/"javascript/" type=/"text/javascript/">document.{0}_data = {1}</script>", MakeUniqueID(null), ToJsStringArray(AutoCompleteData)));
            }

            #endregion
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            output.WriteLine(string.Format("<div onmouseleave=/"document.getElementById('{0}').style.display = 'none';/" style=/"width:{1}/" >", MakeUniqueID("autoCompleteFrame"), Width));
            textBox.RenderControl(output);
            output.WriteLine("<br />");
            autoCompleteFrame.RenderControl(output);
            output.WriteLine("</div>");
        }

        #endregion
    }
}
下載

http://teddyma.VEVb.com/Files/teddyma/AutoCompleteTextBox.zip


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 天台县| 洛扎县| 徐水县| 洛宁县| 灵川县| 双桥区| 永城市| 新宾| 东兰县| 瓦房店市| 平江县| 丹阳市| 武穴市| 石首市| 蒙山县| 泽普县| 白银市| 邮箱| 廊坊市| 格尔木市| 安阳市| 睢宁县| 文化| 贡觉县| 阜宁县| 东乡县| 南郑县| 壶关县| 阜南县| 新宾| 酒泉市| 永平县| 桑日县| 资中县| 绿春县| 东源县| 宁城县| 上思县| 广河县| 进贤县| 措美县|