javascript - 使用 Ajax 实现搜索框

标签 javascript asp.net ajax ado.net search-box

我是 ASP.NET 的新手,我正在使用 AJAX 在我的应用程序中创建一个搜索框。

例如:如果用户在文本框中输入“abc”,那么文本框会从数据库中获取以“abc”开头的数据。

但是,我看不到数据,

这是我的代码片段:

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

<!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>
<script type="text/javascript">
    function getdata()
     {

         var connection = new ActiveXObject("System.Data.SqlClient");

         var connectionstring = "Data Source=ilsql;Initial Catalog=krunal_DB;User ID=krunaldbuser;Password=krunal@2012;Provider=System.Data.SqlClient";

        connection.Open(connectionstring);
        var rs = new ActiveXObject("ADODB.Recordset");

        rs.Open("SELECT DISTINCT Scrip FROM dbo.SearchBoxData where Scrip Like '{0}%'", TextBox1.Text, connection);
        rs.MoveFirst
        while (!rs.eof) {

            document.write(rs.fields(1));
            rs.movenext;
        }

        rs.close;
        connection.close;





            var xmlhttp;
            if (str.length == 0) {
                document.getElementById("txtHint").innerHTML = "";
                return;
            }
            if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            }
            else {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET", "gethint.asp?q=" + str, true);
            xmlhttp.send();

    }
</script>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" onkeyup="getdata()"></asp:TextBox>

    </div>
    </form>
</body>
</html>

我们将不胜感激。

提前致谢!!

最佳答案

为什么不试试 AjaxControlToolkit 的 AutoCompleteExtender。找到:demo here

<ajaxToolkit:AutoCompleteExtender 
runat="server" 
ID="autoComplete1" 
TargetControlID="myTextBox"
ServiceMethod="GetCompletionList"
ServicePath="AutoComplete.asmx"
MinimumPrefixLength="2" 
CompletionInterval="1000"
EnableCaching="true"
CompletionSetCount="20" 
CompletionListCssClass="autocomplete_completionListElement" 
CompletionListItemCssClass="autocomplete_listItem" 
CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem"
DelimiterCharacters=";, :"
ShowOnlyCurrentWordInCompletionListItem="true">
    <Animations>
        <OnShow> ... </OnShow>
        <OnHide> ... </OnHide>
    </Animations>

关于javascript - 使用 Ajax 实现搜索框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10120404/

相关文章:

javascript - 用于缩短页面加载时间的非阻塞样式表

c# - 使用 Dapper QueryAsync 方法时 connection.OpenAsync 和 connection.Open 之间的区别

javascript - AJAX - 发送到另一个 php 文件,同时保持在同一页面上

javascript - 未设置 PHP GET 变量

javascript - 如何在浏览器控制台中记录使用 jQuery AJAX 运行的 PHP 文件?

javascript - 如何忽略文本Angular编辑器中的图像

javascript - 检测 iframe 内的浏览器重新加载

javascript - JavaScript 中的重入

c# - 在 ASP.net 的数据访问层中创建实体的正确方法是什么?

c# - 我应该把 Database.EnsureCreated 放在哪里?