我是ASP.NET新手,我在我的應用程序中使用AJAX製作搜索框。使用Ajax實現SearchBox
例如:如果用戶在文本框中輸入「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;[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>
任何幫助將Appriciated。
在此先感謝!
你爲什麼不從AjaxControlToolkit使用自動完成文本框?它真的很容易使用和配置 – Habib 2012-04-12 09:01:14