Ajax学习札记(一)AutoCompleteExtender控件

AutoCompleteExtender控件

 1、普通

首先当让是要添加所需要的东西了,多的不说了,代码如下:

   <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <cc1:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server"
                    DelimiterCharacters="" Enabled="True" ServicePath="googleSaByDatabase.asmx"
                   ServiceMethod="GetCompletionList" MinimumPrefixLength ="1"
    TargetControlID="TextBox1">
                </cc1:AutoCompleteExtender>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>

ServicePath:说明为此提供服务的文件

ServiceMethod:说明提供服务的方法

MinimumPrefixLength :说明开始提示的最少字符数,默认为3。

然后添加一个web服务googlesa.asmx,其代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
///googlesa 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class googlesa : System.Web.Services.WebService {

    public googlesa () {

        //如果使用设计的组件,请取消注释以下行
        //InitializeComponent();
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
    [WebMethod]
    public string[] GetCompletionList(string prefixText, int count)
    {
        if (count == 0)
        {
            count = 10;
        }
        if (prefixText.Equals("x"))
        {
          return new string[0];
        }
        List<string> items = new List<string>(count);
        Random random1 = new Random();
        for (int i = 0; i < count; i++)
        {
            char c1 = (char)random1.Next(65, 97);
            char c2 = (char)random1.Next(97,122);
            char c3 = (char)random1.Next(97,122);
            items.Add(prefixText +c1+c2+c3 );
        }
        return items.ToArray();
    }
   
}

2、从数据库中提取数据,代码如下:

 <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <cc1:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server"
                    DelimiterCharacters="" Enabled="True" ServicePath="googleSaByDatabase.asmx"
                   ServiceMethod="GetWordList" MinimumPrefixLength ="1"
    TargetControlID="TextBox1">
                </cc1:AutoCompleteExtender>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>

同上一样建立一个web服务googleSaByDatabase.asmx,代码如下:

using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
/// <summary>
///googleSaByDatabase 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class googleSaByDatabase : System.Web.Services.WebService
{

    public googleSaByDatabase()
    {

        //如果使用设计的组件,请取消注释以下行
        //InitializeComponent();
    }
    public static string[] m_autoCompleteWordList = null;
    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }
    [WebMethod]
    public string[] GetWordList(string prefixText, int count)
    {

        SqlConnection myCn = new SqlConnection("Data Source=(local);Initial Catalog=DataBase_dbms;Persist Security Info=True;User ID=sa;Password=*******");
        List<string> items = new List<string>(count);//泛型
        try
        {
            myCn.Open();//打开数据库连接
            SqlCommand myCmd = new SqlCommand("select top " + count + " S_name from Student where S_name like '" + prefixText + "%'group by S_name order by S_name ", myCn);
            SqlDataReader myDR = myCmd.ExecuteReader();
            while (myDR.Read())
            {
                items.Add(myDR["S_name"].ToString());
            }
            myCn.Close();//关闭数据库连接
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return items.ToArray();


    }
}

posted @ 2009-08-30 16:21  Libo@Deng  阅读(476)  评论(0编辑  收藏  举报