一、采用webserver方法,此方法简单,易理解,但效率太代,在这里我们只提一下:
1.webserver中的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
namespace WebApplication20
{
/// <summary>
/// WebService1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string[] GetCompleteList(string prefixText,int count)
{
List<string> items = new List<string>(count);
SqlConnection myCon = new SqlConnection("server=.;database=OAsystem;uid=sa;pwd=123");
myCon.Open();
SqlCommand myCmd = new SqlCommand("select top " + count + " mark from personal_getsendinfo where mark like '" + prefixText + "%'", myCon);
SqlDataReader myDR = myCmd.ExecuteReader();
while (myDR.Read())
{
items.Add(myDR["mark"].ToString());
}
myCon.Close();//关闭数据库连接
return items.ToArray();
}
}
}
2.主页面代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication20._Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!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>
<form id="form1" runat="server">
<div>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server"
DelimiterCharacters="" Enabled="True" MinimumPrefixLength="1"
ServiceMethod="GetCompleteList" ServicePath="WebService1.asmx"
TargetControlID="TextBox1" UseContextKey="True" CompletionSetCount="30"
CompletionInterval="100"/>
</div>
</form>
</body>
</html>
就这样,这种方法太简单了,效率太代,我不推荐,
二、js+jq肯定是大家梦寐已求的方法,现在我们来介绍一下,这种方法的做法,一样看代码:做为一名程序就不要被代码给吓倒:
Default.aspx页面后台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="baiduselect._Default" %>
<!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>
<style type="text/css">
#d1
{
border: 1px solid #999999;
padding-top: 0px;
width: 240px;
}
#d1 input
{
height: 18px;
margin-top: 0px;
}
#d1 #txt1
{
width: 238px;
border: 0px;
border-bottom: 1px solid #999999;
}
#d
{
font-size: 12px;
color: #333333;
}
#d div
{
float: left;
}
#d2
{
padding-top: 0px;
width: 100%;
display: none;
}
#d2 table
{
width: 100%;
}
#d2 table tr td:hover
{
background-color: Blue;
cursor: pointer;
color: White;
}
</style>
<script src="JS/jquery-1.3.2.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
var bool;
$("#txt1").keyup(function() {
var emailcontent = $.trim($("#txt1").val());
if (emailcontent.length == 0) {
$("#d2").hide();
}
else {
$.ajax({
type: "POST",
url: "displayemailcontent.aspx",
data: { emailcontent: emailcontent },
success: function(msg) {
$("#d2").show();
$("#d2").html(msg);
$("table tr td").click(function() {
$("#txt1").val($(this).html());
$("#d2").hide();
})
.hover(function() {
bool = 0;
}, function() {
bool = 1;
})
}
})
}
})
.blur(function() {
if (bool == 1) {
$("#d2").hide();
}
})
})
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="d">
<div id="d1">
<input type="text" id="txt1" />
<div id="d2">
</div>
</div>
</div>
</form>
</body>
</html>
以上我用的是jq中的ajax方法很常用啊!大家记住了哦!
displayemailcontent.aspx代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
namespace baiduselect
{
public partial class displayemailcontent : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string emailcontent=Request["emailcontent"].ToString();
string ConString = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
SqlConnection conn = new SqlConnection(ConString);
string sql =string.Format("select top 10 emailcontent from personal_getsendinfo where emailcontent like '{0}'+'%'",emailcontent);
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
DataSet ds=new DataSet();
sda.Fill(ds);
DataTable dt = ds.Tables[0];
StringBuilder sb = new StringBuilder();
sb.Append("<table>");
for (int i = 0; i < dt.Rows.Count; i++)
{
sb.Append("<tr><td>"+dt.Rows[i]["emailcontent"].ToString()+"</td></tr>");
}
sb.Append("</table>");
Response.Write(sb);
Response.End();
}
}
}
完毕,为感谢大家对我的支持,我将第二种方法的压缩发到了csdn请大家去下载