zoukankan      html  css  js  c++  java
  • AJAX中利用AutoCompleteExtender实现类似于谷歌的智能提示(利用Access数据库)

    1、先打开Access数据库,建立如图所示的表:

    2、打开VS新建WebForm1.aspx窗体和WebService1.asmx窗体。

    3、在WebService1.asmx后台代码中添加以下方法:

    (和SQL连接一样先把[System.Web.Script.Services.ScriptService]的注释撤销)

    在WebService1.asmx添加一个getText方法
     [WebMethod]
            public string[] getText(string prefixText, int count)
            {
                string[] auto = null;
                if (string.IsNullOrEmpty(prefixText) == true || count <= 0)
                    return null;
                if (auto == null)
                {
                    OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(".\\App_Data\\Text.mdb"));
                    string strsql = "select address from Address where address like '%" + prefixText + "%'";
                    OleDbCommand cmd = new OleDbCommand(strsql, conn);
                    conn.Open();
                    OleDbDataReader dr = cmd.ExecuteReader();
                    ArrayList list = new ArrayList();
                    while (dr.Read())
                    {
                        list.Add(dr[0].ToString());
    
                    }
                    dr.Close();
                    string[] temp = new string[list.Count];
                    int i = 0;
                    foreach (string s in list)
                    {
                        temp[i++] = s;
                    }
                    auto = temp;
                }
                return auto;
            }

    4、在WebForm1.aspx窗体中添加ScriptManager控件、TextBox控件、AutoCompleteExtender控件,源代码如下所示:

    WebForm1.aspx源码
    <body>
        <form id="form1" runat="server">
     
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server" 
            MinimumPrefixLength="1" TargetControlID="TextBox1" ServiceMethod="getText" ServicePath="~/WebService1.asmx" CompletionSetCount="14" FirstRowSelected="True">
        </asp:AutoCompleteExtender>
        <br />
     
        </form>
    </body>

    5、在浏览器中查看如图所示:

  • 相关阅读:
    批量清理java源码的target目录
    前端移动node_modules到其他位置
    oracle物化视图和视图的数据不一致
    百词斩英语单词素材提取、听力练习
    2048自动游戏AI, 最高可以玩出一二十个2048
    switcheroo: Alt+Tab的替代工具、窗口搜索
    为知笔记wiz.editor.md增强
    腾讯北极星 Polaris 试用
    [分布式] 分布式事务、seata
    Mysql查询所有的表名和查询表中所有的字段名
  • 原文地址:https://www.cnblogs.com/wynet/p/2777629.html
Copyright © 2011-2022 走看看