zoukankan      html  css  js  c++  java
  • vs2008 asp.net ajax控件实现百度,谷歌智能搜索

    首先创建一个webservice,不懂也没关系,会用就ok。

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    using System.Xml.Linq;
    using System.Data.SqlClient;
    using System.Configuration;//引用

     /// <summary>
        /// KeyFind 的摘要说明
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [ToolboxItem(false)]
        // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
        [System.Web.Script.Services.ScriptService]
        public class KeyFind : System.Web.Services.WebService
        {
            public KeyFind()
            {
                //如果使用设计的组件,请取消注释以下行
                //InitializeComponent();
            }
            //定义数组保存获取的内容

            private string[] autoCompleteWordList = null;

            //两个参数“prefixText”表示用户输入的前缀,count表示返回的个数

            [WebMethod]

            //方法名要与调用次方法的控件重名
            public String[] GetCompleteDepart(string prefixText, int count)
            {


                //检测参数是否为空

                if (string.IsNullOrEmpty(prefixText) == true || count <= 0) return null;

                // 如果数组为空

                if (autoCompleteWordList == null)
                {

                    //读取数据库的内容

                    //连接数据库字符串
                    string sqlstr = "server=192.168.168.57;database=ClothingManage;uid=sa;pwd=1qaz@WSX";
                    SqlConnection conn = new SqlConnection(sqlstr);
                    //连接数据库
                    conn.Open();

                    string sql = "select ClothingNo from ClothingManage  where ClothingNo like N'%" + prefixText + "%' order by ClothingNo  desc";

                    SqlDataAdapter da = new SqlDataAdapter(sql, conn);

                    DataSet ds = new DataSet();

                    da.Fill(ds);

                    //读取内容文件的数据到临时数组

                    string[] temp = new string[ds.Tables[0].Rows.Count];

                    int i = 0;

                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {

                        temp[i] = dr["ClothingNo"].ToString();

                        i++;

                    }

                    Array.Sort(temp, new CaseInsensitiveComparer());

                    //将临时数组的内容赋给返回数组

                    autoCompleteWordList = temp;

                    if (conn.State == ConnectionState.Open)

                        conn.Close();

                }

                //定位二叉树搜索的起点

                int index = Array.BinarySearch(autoCompleteWordList, prefixText, new CaseInsensitiveComparer());

                if (index < 0)
                {   //修正起点

                    index = ~index;

                }

                //搜索符合条件的数据

                int matchCount = 0;

                for (matchCount = 0; matchCount < count && matchCount + index < autoCompleteWordList.Length; matchCount++)
                {   //查看开头字符串相同的项

                    //这里为什么屏蔽掉。如果不屏蔽掉就不是完全的智能搜索。比如E0010 如果我输入0,那么就不会显示。如果有屏蔽掉了就会//

                    //有该数据

                    //if (autoCompleteWordList[index + matchCount].StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase) == false)
                    //{

                    //    break;

                    //}

                }

                //处理搜索结果

                string[] matchResultList = new string[matchCount];

                if (matchCount > 0)
                {   //复制搜索结果

                    Array.Copy(autoCompleteWordList, index, matchResultList, 0, matchCount);

                }

                return matchResultList;


            }
        }

    --------------------------------------------------------------------------------------------

    //AjaxControlToolkit.Binary.NET35.zip  这个是ajax控件集。网上一大片,我就不再重复了。

    解压完成之后,然后再引用里面添加就行了。然后再左边添加“选项卡”名字随意。然后打开你刚才的选项卡,在选择“选择项”,然后找到你解压的文件夹。添加dll文件,左边会自动生成控件。 叫AjaxControlToolkit.dll。 对,就是这么简单!

     前台:在引用的页面添加此控件,如果是<asp:ScriptManager /> 则会引出版本问题。

    再你需要添加文本框的后面添加扩张功能 :选择这个:AutoCompleteExtender

      

            <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
            </asp:ToolkitScriptManager>

      <asp:TextBox ID="tbClothingNo" runat="server" onblur="onTextBoxBlur()"></asp:TextBox>
                            <asp:AutoCompleteExtender ID="tbClothingNo_AutoCompleteExtender" runat="server" DelimiterCharacters=";:,"
                                Enabled="True" ServicePath="~/Web/WebService/KeyFind.asmx" TargetControlID="tbClothingNo"
                                CompletionSetCount="10" MinimumPrefixLength="1" ServiceMethod="GetCompleteDepart">

    ServiceMethod:你调用的webservice方法名

     CompletionSetCount 显示条数

    DelimiterCharacters=";:," 需要屏蔽的符号

    TargetControlID //需要智能搜索控件的ID

    ServicePath //路径  

    MinimumPrefixLength //搜索最小的长度。当大于等于1的时候搜索

    webserviece叫啥名字无关。关键是你自己要知道就ok。

    就这几点重要的

    另外我在网上找了一些代码,都是过时的,没有针对最新版本的。让我相当纠结,所以我在此发布一个vs2008最新版本的。

    我已经测试过,没有任何问题。有不懂得请留言。或者发邮箱给我 xugan1122@qq.com 

  • 相关阅读:
    HTML中的文本标签
    Java 数组的创建
    JavaScript实现LUHN算法验证银行卡号有效性
    JavaScript实现HTML页面集成QQ空间分享功能
    JavaScript中的三种弹出框的区别与使用
    Maven 项目中的 pom.xml 文件内容说明
    FTPClient 中 FTPClient.changeWorkingDirectory(filePath) 代码一直返回 false
    Eclipse 中 Debug 时鼠标悬停无法查看变量值
    Innodb ,MyISAM
    tomcat jetty
  • 原文地址:https://www.cnblogs.com/softwareking/p/2076049.html
Copyright © 2011-2022 走看看