Using Ajax to realize the simple simulation of Google input automatic completion

Source: Internet
Author: User
Tags copy count return string


Relatively simple simulation, text box input CompanyName, and then
Search the CompanyName field in the SqlServer2000 Northwind database Suppliers table,
And then the implementation is done automatically
Four files
1. Autocomplete.htm


[Run code] [Copy to Clipboard] [ ± ] CODE:


Input Auto Completion



Companyname
of the dropdown table


Autocomplete.aspx
<%@ Page language= "C #" autoeventwireup= true "codebehind=" AutoComplete.aspx.cs "inherits=" Ajaxbasehome.autocomplete "%>

AutoComplete.aspx.cs

[Run code] [Copy to Clipboard] [ ± ] CODE:
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 (Input) (getcompanyname);
}

Gets the input string
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 file for displaying XML data


[Run code] [Copy to Clipboard] [ ± ] CODE:


<TD style= "Cursor:hand" >


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.