Click here to Skip to main content
15,903,385 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Sample.aspx:
XML
<%@ 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>


AutoComplete.ashx
C#
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&lt;AutoCompleteDTO&gt; 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&lt;AutoCompleteDTO&gt;();
                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;
        }
    }
}

AutoCompleteDIO.cs
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// &lt;summary&gt;
/// Summary description for AutoCompleteDTO
/// &lt;/summary&gt;
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;
        }
    }
}
Posted
Updated 16-Feb-11 6:11am
v2
Comments
Vineet_2109 16-Feb-11 8:16am    
Above page is Sample.aspx
Abdul Quader Mamun 16-Feb-11 19:13pm    
where is the question?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900