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

添加人:admin三级(1325分)   添加时间:2007-07-10    阅读次数:8365  收藏此教程
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();
            }
        }
    }
}

xslt文件 用于显示xml数据
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>
  <xsl:template match="/">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="NewDataSet">
    <table id="tblContent" style="background-color:GrayText">
    <xsl:for-each select="Table">
      <tr>
        <!--td中单击时选择当前值, 鼠标在上时更改行背景颜色,鼠标离开后清除背景颜色-->
        <td onclick="selValue()" style="cursor:hand" onmouseover="clearColor();this.parentElement.style.backgroundColor='InfoBackground'" onmouseout="clearColor()">
          <xsl:value-of select="CompanyName"/>
        </td>
      </tr>
    </xsl:for-each>
    </table>
  </xsl:template>
</xsl:stylesheet>
2页 第2上一页12下一页
相关的教程: ajax 自动完成 google
收藏此教程

当前平均分: 1.1(15 次打分)

-5-4-3-2-1012345
评论主题
您的大名
您的评论
验证码 点击换一个验证码
知识库搜索: