偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

ASP.NET Ajax中AutoComplete控件的使用

開發(fā) 后端 開發(fā)工具
大家一定見過(guò)google和迅雷等網(wǎng)站在搜索文本框中輸入文字后能自動(dòng)提示,你會(huì)不會(huì)感覺(jué)這種功能很炫也很實(shí)用.實(shí)際上在學(xué)習(xí)asp.net ajax之后,通過(guò)AjaxControlToolKit工具包中的AutoComplete控件也能實(shí)現(xiàn)這種功能,而且非常簡(jiǎn)單。本文作者將教你如何來(lái)實(shí)現(xiàn)這個(gè)功能。

簡(jiǎn)介

AutoComplete控件就是在用戶在文本框輸入前幾個(gè)字母或是漢字的時(shí)候,該控件就能從存放數(shù)據(jù)的文或是數(shù)據(jù)庫(kù)里將所有以這些字母開頭的數(shù)據(jù)提示給用戶,供用戶選擇,提供方便。

重要屬性

1.TargetControlID:指定要實(shí)現(xiàn)提示功能的控件;

2.ServicePath:WebService的路徑,提取數(shù)據(jù)的方法是寫在一個(gè)WebService中的;

3.ServeiceMethod:寫在WebService中的用于提取數(shù)據(jù)的方法的名字;

4.MinimumPrefixLength:用來(lái)設(shè)置用戶輸入多少字母才出現(xiàn)提示效果;

5.CompletionSetCount:設(shè)置提示數(shù)據(jù)的行數(shù);

6.CompletionInterval:從服務(wù)器獲取書的時(shí)間間隔,單位是毫秒。

示例

打開vs2005創(chuàng)建一個(gè)AjaxControlToolKit網(wǎng)站。

在網(wǎng)站的App_Data文件夾下添加文本文件TextFile.txt,并在其中添加數(shù)據(jù),如下:

在網(wǎng)站的根目錄下添加一個(gè)Web服務(wù),命名為oec2003_AutoComplete,系統(tǒng)自動(dòng)將Web服務(wù)兩個(gè)部分,設(shè)計(jì)部分oec2003_AutoComplete.asmx和代碼部分oec2003_AutoComplete.cs,其中oec2003_AutoComplete.cs文件自動(dòng)放入到App_Code目錄下。打開oec2003_AutoComplete.cs文件,添加獲取數(shù)據(jù)的方法GetCompleteList,代碼如下:

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;

 
/// <summary>
/// AutoComplete 的摘要說(shuō)明
/// <summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService {

    public AutoComplete () {

        //如果使用設(shè)計(jì)的組件,請(qǐng)取消注釋以下行 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
    /// <summary>
    /// 獲取數(shù)據(jù)的方法GetCompleteList
    /// <summary>
    //定義靜態(tài)數(shù)組用于保存獲取的數(shù)據(jù)
    private static string[] autoCompleteWordList = null;
    [WebMethod]
    public String[] GetCompleteList(string prefixText, int count)
    {
        if (autoCompleteWordList == null)
        {
            string[] temp = File.ReadAllLines(Server.MapPath("~/App_Data/TextFile.txt"));
            Array.Sort(temp, new CaseInsensitiveComparer());
            autoCompleteWordList = temp;
        }

        int index = Array.BinarySearch(autoCompleteWordList, prefixText, new CaseInsensitiveComparer());
        if (index < 0)
        {
            index = ~index;
        }

        int matchingCount;
        for (matchingCount = 0; matchingCount < count && index + matchingCount < autoCompleteWordList.Length; matchingCount++)
        {
            if (!autoCompleteWordList[index + matchingCount].StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase))
            {
                break;
}
        }
        String[] returnValue = new string[matchingCount];
        if (matchingCount > 0)
       {
           Array.Copy(autoCompleteWordList, index, returnValue, 0, matchingCount);
        }
        return returnValue;
    }

}

由于在上面的代碼中使用了File類,所以應(yīng)該添加如下代碼:

using System.IO;

因?yàn)樾枰诳蛻舳苏{(diào)用Web服務(wù),還需要添加如下代碼:

[System.Web.Script.Services.ScriptService]

保存Web 服務(wù)的代碼

打開根目錄下默認(rèn)生成的Default.aspx

在頁(yè)面中拖拽一個(gè)TextBox控件和一個(gè)AutoCompleteExtender控件。

在屬性窗口設(shè)置AutoCompleteExtender控件的屬性,如下

<ajaxToolkit:AutoCompleteExtender 
            ID="AutoCompleteExtender1" 
            runat="server" 
            ServiceMethod="GetCompleteList" 
            ServicePath="oec2003_AutoComplete.asmx" 
            Enabled="true" 
            MinimumPrefixLength="2" 
               CompletionSetCount="10"
            TargetControlID="TextBox1">
</ajaxToolkit:AutoCompleteExtender>

在Web服務(wù)中的count參數(shù)的值是取CompletionSetCount屬性的值。

保存設(shè)計(jì)的頁(yè)面,將默認(rèn)頁(yè)面設(shè)置為起始頁(yè),按F5運(yùn)行后在文本框中輸入oe,就能看到想要的結(jié)果。

【編輯推薦】

  1. ASP.NET MVC案例教程
  2. ASP.NET MVC教程:創(chuàng)建TaskList應(yīng)用程序
  3. ASP.NET中URL Rewrite的實(shí)現(xiàn)方法
責(zé)任編輯:楊鵬飛 來(lái)源: 博客園
相關(guān)推薦

2009-07-28 16:21:03

Asp.net AjaAutoComplet

2009-07-21 17:18:26

UpdateProgrASP.NET AJA

2009-07-20 13:54:31

ScriptManagASP.NET AJA

2009-07-29 13:50:26

UpdatePanelASP.NET

2009-07-21 17:27:12

UpdateProgrASP.NET AJA

2009-07-21 09:18:02

UpdatePanelASP.NET AJA

2009-01-16 13:17:16

AjaxASP.NET.NET

2009-07-30 12:19:32

ASP.NET中使用A

2009-07-23 16:44:51

AdRotator控件ASP.NET

2009-07-24 15:35:00

ASP.NET Gri

2009-09-11 09:09:00

ASP.NETAdRotator控件

2009-07-20 13:32:24

ScriptManagASP.NET

2009-07-27 09:07:04

Profile SerASP.NET AJA

2009-07-20 17:59:07

JavaScript調(diào)ASP.NET AJA

2009-07-22 16:11:43

ASP.NET AJA

2009-07-22 16:17:39

ASP.NET AJA

2009-07-22 16:25:41

ASP.NET AJA

2009-07-24 13:41:15

ASP.NET AJA

2009-07-22 16:05:34

ASP.NET AJA

2009-07-29 13:32:06

ASP.NET控件使用
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)