前台html:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="jquery-1.3.2-vsdoc2.js" type="text/javascript"></script>
<script src="jquery/autocomplete/jquery.autocomplete.min.js" type="text/javascript"></script>
<link href="jquery/autocomplete/jquery.autocomplete.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
$().ready(function() {
$("#t_person").focus().autocomplete("ajax_do.aspx?page=texttishi&dotype=1", {
max:1000,
150,
selectFirst: false
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="t_person" />
</div>
</form>
</body>
</html>
<title></title>
<script src="jquery-1.3.2-vsdoc2.js" type="text/javascript"></script>
<script src="jquery/autocomplete/jquery.autocomplete.min.js" type="text/javascript"></script>
<link href="jquery/autocomplete/jquery.autocomplete.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
$().ready(function() {
$("#t_person").focus().autocomplete("ajax_do.aspx?page=texttishi&dotype=1", {
max:1000,
150,
selectFirst: false
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="t_person" />
</div>
</form>
</body>
</html>
后台(ajax_do.aspx.cs):
using (SqlConnection con = new SqlConnection(GClass.connMyBo))
{
string strRet = "";
string key = Request.Params["q"].ToString();
string cmdSel = "select party_id from person where party_id like @key";
SqlCommand cmd = new SqlCommand(cmdSel, con);
con.Open();
cmd.Parameters.AddWithValue("key", "%" + key + "%");
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
strRet +=reader[0].ToString()+"\n";
}
Response.Write(strRet);
}
{
string strRet = "";
string key = Request.Params["q"].ToString();
string cmdSel = "select party_id from person where party_id like @key";
SqlCommand cmd = new SqlCommand(cmdSel, con);
con.Open();
cmd.Parameters.AddWithValue("key", "%" + key + "%");
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
strRet +=reader[0].ToString()+"\n";
}
Response.Write(strRet);
}
备注:
autocomplete默认的关键字是q,后台可以直接用Request.Params["q"]来获取当前输入文本框的值。