ITeye博客

用AJAX实现google输入自动完成的简单模拟

makuixuan · 2010-12-30 15:40 · Ajax · Web前端 · Google · Ajax · UI · XML · Web · Markdown

比较简单的模拟,文本框输入CompanyName,然后 搜索SqlServer2000 里NorthWind数据库 Suppliers表的CompanyName字段, 然后实现自动完成 四个文件 [code="1 .AutoComplete.htm"] 输入自动完成 CompanyName

[/code] [code="AutoComplete.aspx"] <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AutoComplete.aspx.cs" Inherits="AJAXBaseHome.AutoComplete" %> AutoComplete.aspx.cs using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Collections; using System.IO; using System.Text; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Web.Configuration; namespace AJAXBaseHome { public partial class AutoComplete : System.Web.UI.Page { private static string conString = WebConfigurationManager.ConnectionStrings["myData"].ConnectionString; protected void Page_Load(object sender, EventArgs e) { string input = GetInput(); Response.Write(GetCompanyName(input)); } //获取输入的字符串 private string GetInput() { Stream s = Request.InputStream; int count = 0; byte[] buffer = new byte[1024]; StringBuilder builder = new StringBuilder(); while ((count = s.Read(buffer, 0, 1024)) > 0) { builder.Append(Encoding.UTF8.GetString(buffer, 0, count)); } return builder.ToString(); } private string GetCompanyName(string input) { using (SqlConnection con = new SqlConnection(conString)) { SqlCommand command = new SqlCommand("select * from suppliers where CompanyName like @Name", con); command.Parameters.Add(new SqlParameter("@name", input + "%")); SqlDataAdapter adapter = new SqlDataAdapter(command); DataSet ds = new DataSet(); adapter.Fill(ds); return ds.GetXml(); } } } } [/code] [code="xslt文件 用于显示xml数据"]

[/code]

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。