<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Sample.aspx.cs" Inherits="Sample" %> <%@ Import Namespace="System.Web.Services" %> <!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 id="Head1" runat="server"> <title></title> <link href="CSS/jquery-ui[1].css" rel="stylesheet" type="text/css" /> <script src="jquery/jquery.min.js" type="text/javascript"></script> <script src="jquery/jquery-1.4.1.min.js" type="text/javascript"></script> <script src="jquery/jquery-ui.min.cache.js" type="text/javascript"></script> <%--<script src="jquery/jquery.min.js" type="text/javascript"></script>--%> <%-- <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/themes/base/jquery-ui.css" type="text/css" media="all" /> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.min.js" type="text/javascript"></script>--%> <script type="text/javascript"> $(function () { $('#<%= txtName.ClientID %>').autocomplete({ source: "AutoComplete.ashx" }); }); </script> <script type="text/javascript"> $(function () { $('#<%= txtName.ClientID %>').autocomplete({ source: "AutoComplete.ashx" }) //$("#textbox").get(0).value = "blah" // .data( "autocomplete" )._renderItem = function( ul, item ) { // return $( "<li></li>" ) // // .data( "item.autocomplete", item ) }); // $('#<%= txtLabel.ClientID %>').val($(this).html())="blah"; // $(function () { //$("input#textbox").val($(this).html()); // $('#<%=txtName.ClientID %>').text({ // source: "GetName.ashx" // }); // }); </script> <script type="text/javascript"> $(function () { $('#<%= txtCountries.ClientID %>').autocomplete({ source: "Countries.ashx" }); }); </script> </head> <body> <form id="form1" runat="server"> <div> Continent : <asp:TextBox ID="txtName" runat="server"></asp:TextBox> <br /> Countries : <asp:TextBox ID="txtCountries" runat="server"></asp:TextBox> </div> </form> </body> </html>
using System; using System.Collections.ObjectModel; using System.Data; using System.Data.SqlClient; using System.Web; using System.Web.Script.Serialization; using System.Collections.Generic; using System.Linq; using System.Web.UI; using System.Web.UI.WebControls; public class AutoComplete : IHttpHandler { public void ProcessRequest(HttpContext context) { string searchText = context.Request.QueryString["term"]; Collection<AutoCompleteDTO> collection; string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Cas.mdf;Integrated Security=True;User Instance=True"; using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand command = new SqlCommand()) { command.Connection = connection; command.CommandText = "Search_Users"; command.CommandType = CommandType.StoredProcedure; SqlParameter paramName = new SqlParameter("@ContinentName", SqlDbType.VarChar); paramName.Size = 100; paramName.Value = searchText; command.Parameters.Add(paramName); collection = new Collection<AutoCompleteDTO>(); connection.Open(); using (SqlDataReader dataReader = command.ExecuteReader()) { AutoCompleteDTO dto; if (dataReader != null) while (dataReader.Read()) { dto = new AutoCompleteDTO(); dto.value = dto.label = (string)dataReader["ContinentName"]; dto.id = Convert.ToString(dataReader["ID"]); collection.Add(dto); dto.cid = Convert.ToString(dataReader["ID"]); } } } } JavaScriptSerializer serializer = new JavaScriptSerializer(); string jsonString=serializer.Serialize(collection); context.Response.Write(jsonString); } public bool IsReusable { get { return false; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Summary description for AutoCompleteDTO /// </summary> public class AutoCompleteDTO { string CID; public string id { get; set; } public string label { get; set; } public string value { get; set; } public string cid { get { return CID; } set { value = CID; } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)