ajaxControlToolkitAutoCompleteExtender的用法_javascript技巧
來源:懂視網
責編:小采
時間:2020-11-27 20:40:27
ajaxControlToolkitAutoCompleteExtender的用法_javascript技巧
ajaxControlToolkitAutoCompleteExtender的用法_javascript技巧:AutoCompleteExtender 自動完成擴展, 配合TextBox使用功能類似現在google中輸入搜索字,則在TextBox下出來下拉框顯示搜索目標中的項目 這個擴展控件需要配合Web Service使用,所以涉及了點web Service的使用(這里只簡單談下,等用熟了再仔細談下web
導讀ajaxControlToolkitAutoCompleteExtender的用法_javascript技巧:AutoCompleteExtender 自動完成擴展, 配合TextBox使用功能類似現在google中輸入搜索字,則在TextBox下出來下拉框顯示搜索目標中的項目 這個擴展控件需要配合Web Service使用,所以涉及了點web Service的使用(這里只簡單談下,等用熟了再仔細談下web

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]方法
[WebMethod]
代碼如下:
public string[] GetString(string prefixText, int count){
System.Collections.Generic.List
list = new System.Collections.Generic.List(count);
System.Data.DataSet ds = new System.Data.DataSet();
//這里是我在數據庫中取數據的代碼 其中SqlHelper類是項目中的取數據基類
//string strSql = string.Format("SELECT TOP {0} NAME FROM CengWei WHERE NAME LIKE '{1}%' ORDER BY NAME",count,prefixText);
//ds = SqlHelper.Query(strSql);
//for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
//{
// list.Add(ds.Tables[0].Rows[i][0].ToString());
//}
for (int i = 0; i < count; i++)
{
list.Add(prefixText+i.ToString());
}
return list.ToArray();
}
其中:必須在webService的類上面添加
[System.Web.Script.Services.ScriptService]
示例代碼:webService是在數據庫中的一個字段中取數據
頁面代碼:
代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test2.aspx.cs" Inherits="test2" %>
<%@ Register Assembly="CrystalDecisions.Web, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"
Namespace="CrystalDecisions.Web" TagPrefix="CR" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
DropDownExtender簡單練習
rel="stylesheet" type="text/css" />
webService代碼:
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
///
/// AutoComplete 的摘要說明
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//下面是必須的,否則功能無法實現
[System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService {
public AutoComplete () {
//如果使用設計的組件,請取消注釋以下行
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public string[] GetString(string prefixText, int count){
System.Collections.Generic.List list = new System.Collections.Generic.List(count);
System.Data.DataSet ds = new System.Data.DataSet();
//這里是我在數據庫中取數據的代碼 其中SqlHelper類是項目中的取數據基類
//string strSql = string.Format("SELECT TOP {0} NAME FROM CengWei WHERE NAME LIKE '{1}%' ORDER BY NAME",count,prefixText);
//ds = SqlHelper.Query(strSql);
//for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
//{
// list.Add(ds.Tables[0].Rows[i][0].ToString());
//}
for (int i = 0; i < count; i++)
{
list.Add(prefixText+i.ToString());
}
return list.ToArray();
}
}
有哪里不對的地方還請大家指教
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
ajaxControlToolkitAutoCompleteExtender的用法_javascript技巧
ajaxControlToolkitAutoCompleteExtender的用法_javascript技巧:AutoCompleteExtender 自動完成擴展, 配合TextBox使用功能類似現在google中輸入搜索字,則在TextBox下出來下拉框顯示搜索目標中的項目 這個擴展控件需要配合Web Service使用,所以涉及了點web Service的使用(這里只簡單談下,等用熟了再仔細談下web