ajaxControlToolkit---AutoCompleteExtender的簡單用法

昨天在搜索中使用了這個控件,不過不知道爲什麼在IE中反應比較慢

 

AutoCompleteExtender 自動完成擴展, 配合TextBox使用功能類似現在google中輸入搜索字,則在TextBox下出來下拉框顯示搜索目標中的項目

這個擴展控件需要配合Web Service使用,所以涉及了點web Service的使用(這裏只簡單談下,等用熟了再仔細談下web service的內容)

先介紹下AutoCompleteExtender的幾個關鍵屬性:

a,TargetControlID 這個屬性是所有AjaxControlToolkit的共同屬性,就是擴展目標控件ID(官方這麼說的吧)

b.CompletionSetCount 這個屬性是設置顯示下拉結果的條數 默認爲10吧

c.MinimumPrefixTextLength 這個屬性是設置輸入幾個字符的長度後調用webService中的方法顯示下拉列表

d.ServicePath  這個屬性設置需要調用的web Service路徑

e.ServiceMethod 這個屬性設置需要調用的web Service中的方法(函數)

f.EnableCaching:是否在客戶端緩存數據,默認爲true

g.CompletionInterval:從服務器讀取數據的時間間隔,默認爲1000,單位:毫秒

 

注:如果習慣用可視控件設置屬性,則a屬性在AutoCompleteExtender中設置,其他屬性則設置了TargetControlId後,在相應的TargetControl中會多出來個Extenders屬性中設置,如果習慣手寫代碼,則在AutoCompleteExtender代碼屬性中設置。

 

例子:  1.新建一個頁面,加入ScriptManager控件 一個TextBox控件 一個AutoCompleteExtender控件

           2.新建立一個webService,添加一個[WebMethod]方法

          

  1. [WebMethod]
  2.     public string[] GetString(string prefixText, int count){
  3.         System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>(count);
  4.         System.Data.DataSet ds = new System.Data.DataSet();
  5.         //這裏是我在數據庫中取數據的代碼 其中SqlHelper類是項目中的取數據基類
  6.         //string strSql = string.Format("SELECT TOP {0} NAME FROM CengWei WHERE NAME LIKE '{1}%' ORDER BY NAME",count,prefixText);
  7.         //ds = SqlHelper.Query(strSql);
  8.         //for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
  9.         //{
  10.         //    list.Add(ds.Tables[0].Rows[i][0].ToString());
  11.         //}
  12.         for (int i = 0; i < count; i++)
  13.         {
  14.             list.Add(prefixText+i.ToString());
  15.         }
  16.         
  17.         return list.ToArray();
  18.     }

其中:必須在webService的類上面添加

[System.Web.Script.Services.ScriptService]

 

示例代碼:webService是在數據庫中的一個字段中取數據

 

頁面代碼:

 

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="test2.aspx.cs" Inherits="test2" %>
  2. <%@ Register Assembly="CrystalDecisions.Web, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"
  3.     Namespace="CrystalDecisions.Web" TagPrefix="CR" %>
  4. <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  6. <html xmlns="http://www.w3.org/1999/xhtml">
  7. <head runat="server">
  8.     <title>DropDownExtender簡單練習</title>
  9.     <link href="/aspnet_client/System_Web/2_0_50727/CrystalReportWebFormViewer3/css/default.css"
  10.         rel="stylesheet" type="text/css" />
  11. </head>
  12. <body>
  13.     <form id="form1" runat="server">
  14.         <div>
  15.             <asp:ScriptManager ID="ScriptManager1" runat="server" EnableScriptGlobalization="True" EnableScriptLocalization="True">
  16.             </asp:ScriptManager>
  17.                     <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
  18.         <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
  19.         <cc1:AutoCompleteExtender ID="AutoCompleteExtender2" runat="server" MinimumPrefixLength="1"
  20.             ServiceMethod="GetString" ServicePath="AutoComplete.asmx" TargetControlID="TextBox2">
  21.         </cc1:AutoCompleteExtender>
  22.     </form>
  23. </body>
  24. </html>

webService代碼:

 

 

  1. using System;
  2. using System.Web;
  3. using System.Collections;
  4. using System.Web.Services;
  5. using System.Web.Services.Protocols;
  6. /// <summary>
  7. /// AutoComplete 的摘要說明
  8. /// </summary>
  9. [WebService(Namespace = "http://tempuri.org/")]
  10. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  11. //下面是必須的,否則功能無法實現
  12. [System.Web.Script.Services.ScriptService]
  13. public class AutoComplete : System.Web.Services.WebService {
  14.     public AutoComplete () {
  15.         //如果使用設計的組件,請取消註釋以下行 
  16.         //InitializeComponent(); 
  17.     }
  18.     [WebMethod]
  19.     public string HelloWorld() {
  20.         return "Hello World";
  21.     }
  22.     [WebMethod]
  23.     public string[] GetString(string prefixText, int count){
  24.         System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>(count);
  25.         System.Data.DataSet ds = new System.Data.DataSet();
  26.         //這裏是我在數據庫中取數據的代碼 其中SqlHelper類是項目中的取數據基類
  27.         //string strSql = string.Format("SELECT TOP {0} NAME FROM CengWei WHERE NAME LIKE '{1}%' ORDER BY NAME",count,prefixText);
  28.         //ds = SqlHelper.Query(strSql);
  29.         //for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
  30.         //{
  31.         //    list.Add(ds.Tables[0].Rows[i][0].ToString());
  32.         //}
  33.         for (int i = 0; i < count; i++)
  34.         {
  35.             list.Add(prefixText+i.ToString());
  36.         }
  37.         
  38.         return list.ToArray();
  39.     }
  40.     
  41. }

有哪裏不對的地方還請大家指教

 

 

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