實現百度搜索輸入框效果

1.添加AjaxControlToolkit.dll(本人博客資源中下載)

2.<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %> 在頁面中引用

3.頁面中具體代碼 

<asp:TextBox ID="txtContactName" runat="server"   Width="200px" Height="20px" MaxLength="100"></asp:TextBox>
     <cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" Enabled="true" TargetControlID="txtContactName" ServicePath="AutoCompleteService.asmx" ServiceMethod="SelectSearchInfo"    CompletionSetCount="10" MinimumPrefixLength="1">
     </cc1:AutoCompleteExtender>

4.添加AutoCompleteService.asmx 文件

     AutoCompleteService.asmx 文件中代碼

public class AutoCompleteService : System.Web.Services.WebService {

    public AutoCompleteService () {

        //如果使用設計的組件,請取消註釋以下行
        //InitializeComponent();
    }

    [WebMethod]
    public string[] SelectSearchInfo(string prefixText,int count)
    {
        ContactList[] lst = ContactList.GetContactName(prefixText.Trim());
      
        if (lst.Length > 0)
        {
            string[] result = new string[lst.Length];

            for (int i = 0; i < lst.Length; i++)
            {
                result[i] = lst[i].CContactName + "; " + lst[i].CCorpName;
            }
            return result;
        }
        else
        {
            string[] result = {"沒有該聯繫人,請先添加聯繫人信息!"};
            return result;
        }
    } 
}

5.頁面效果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章