具有自动完成功能的TextBox控件

作者:herobeast
时间:2007-7-18
/Files/HeroBeast/autocompletetextboxcontrol.rar

我想大家在用163邮箱给朋友写信的时候,已经感受过“自动完成”功能了吧!



下面是我刚做一个类似于那样的控件.如下图:

1.js

 

JS

2.源代码:
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Text;
 5 using System.Web;
 6 using System.Web.UI;
 7 using System.Web.UI.WebControls;
 8 [assembly: WebResource("HBAutoCompleteTextBox.js""application/x-javascript")]
 9 namespace HBControl
10 {
11     [DefaultProperty("Text")]
12     [ToolboxData("<{0}:HBAutoCompleteTextBox runat=server></{0}:HBAutoCompleteTextBox>")]
13     public class HBAutoCompleteTextBox : TextBox
14     {
15        
16         [Bindable(true),
17         Category("Appearance"),
18         Description("备选项")
19         ]
20         public string Items
21         {
22             get 
23             {
24                 string s = (string)ViewState["Items"];
25                 return (s == null? string.Empty : s;
26             }
27             set 
28             {
29                 ViewState["Items"= value; 
30             }
31         }
32         protected override void OnPreRender(EventArgs e)
33         {
34             if (this.Page != null)
35             {
36                 ClientScriptManager mgr = this.Page.ClientScript;
37                 mgr.RegisterClientScriptResource(typeof(HBAutoCompleteTextBox), "HBAutoCompleteTextBox.js");
38             }
39             base.OnPreRender(e);
40             this.Attributes.Add("onkeyup""AutoComplete('" + this.ClientID + "','"+this.Items+"');");
41         }
42     }
43 }
44 
以上实现效果如下:

问题:
 在按回车选择的时候,总是提交页面,很郁闷,还请高手指教!现在先用左右箭头键进行选择的。
posted @ 2007-07-18 17:06  herobeast  阅读(1190)  评论(0)    收藏  举报