2012-04-12 14 views
1

私はASP.NETを初めて使用しています。私は自分のアプリケーションでAJAXを使用して検索ボックスを作っています。Ajaxを使用したSearchBoxの実装

たとえば、ユーザーがテキストボックスに「abc」と入力すると、テキストボックスはデータベースから「abc」で始まるデータをフェッチします。任意のヘルプはにappriciatedされます

<%@ 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;[email protected];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> 

:しかし、私はデータを参照することができません

は、ここに私のコードスニペットです。

ありがとうございました!!

+0

は、なぜあなたはAjaxControlToolkitからオートコンプリートテキストボックスを使用していけませんか?その本当に使いやすく構成する – Habib

答えて

1

なぜ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> 

+0

私はこのAJAX .....この技術の新しい知識、多くの知識を持っていない私のクエリを作成する方法とdb接続を開くには? – Krunal

+0

このlinKにアクセスしてください:http://www.asp.net/ajaxlibrary/act.ashx –

関連する問題