Fork me on GitHub

AutoCompleteExtender实现模乎查询

原来的文章网址:http://atlas.asp.net/docs/atlas/doc/controls/default.aspx#aspnet

Aspx源码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>模乎查询</title>

</head>

<body>

    <form id="form1" runat="server">

      <atlas:ScriptManager runat="server" ID="ScriptManager1" EnablePartialRendering="true" /> 

    <div>

    <h3><u>实现模乎查询</u></h3>

   

            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>   

            <atlas:AutoCompleteExtender runat="server" ID="Extender1" MinimumPrefixLength="1" >

                <atlas:AutoCompleteProperties Enabled="True" TargetControlID="TextBox1" ServiceMethod="GetFilteredList" ServicePath="WebService.asmx" />

            </atlas:AutoCompleteExtender>           

    </div>

    </form>

</body>

</html>

WebService源码

using System;

using System.Web;

using System.Collections;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.Data;

using System.Configuration;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

 

/// <summary>

/// WebService 的摘要说明

/// </summary>

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

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

 

    public WebService () {

 

        //如果使用设计的组件,请取消注释以下行

        //InitializeComponent();

    }

 

    [WebMethod]

    public string[] GetFilteredList(string prefixText)

    {

       DataSet ds = new DataSet();       

        ds.ReadXml(Server.MapPath("JS.xml"));

        DataTable dt = ds.Tables[0];

        DataView dv = dt.DefaultView;

ArrayList items = new ArrayList(dt.Rows.Count);

        for (int i = 0; i < dt.Rows.Count; i++)

        {

            DataRowView drv = dv[i];

            if (drv["zjm"].ToString().ToLower().IndexOf(prefixText.ToLower() ) != -1)

            {

                items.Add(drv["mc"].ToString());

            }

       

        return (string[])items.ToArray(typeof(string));

    }

}

XML文件

<?xml version="1.0" standalone="yes"?>

<NewDataSet>

 <Table>

    <zjm>

      Xg</zjm>

      <mc>西瓜</mc>

      <SQL>

        100

      </SQL>

    </Table>

 <Table>

    <zjm>

      xg</zjm>

      <mc>小瓜</mc>

      <SQL>

        250

      </SQL>

    </Table>

 <Table>

    <zjm>

      mt</zjm>

      <mc>馒头</mc>

      <SQL>

        125

      </SQL>

    </Table>

 <Table>

    <zjm>

      dj</zjm>

      <mc>豆角</mc>

      <SQL>

        254

      </SQL>

    </Table>

 <Table>

    <zjm>

      hd</zjm>

      <mc>红豆</mc>

      <SQL>

        75

      </SQL>

    </Table>

 <Table>

    <zjm>

      hm</zjm>

      <mc>红米</mc>

      <SQL>

        75

      </SQL>

    </Table>

 <Table>

    <zjm>

      ddn</zjm>

      <mc>大豆泥</mc>

      <SQL>

        10075

      </SQL>

    </Table>

 </NewDataSet>

posted @ 2006-07-11 14:30  桂素伟  阅读(584)  评论(0编辑  收藏  举报