Hi fansari6,
I have created a sample which full fill your requirement.
You need to modify the code according to your requirement.
HTML
<table>
<tr>
<td>
Enter search term:
<asp:TextBox ID="txtSearch" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="TxtDiagnosis1" runat="server" CssClass="txtmedicine"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="code1" runat="server" CssClass="txtdosage"></asp:TextBox>
</td>
</tr>
</table>
<div>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" />
<script type="text/javascript" src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script type="" src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script type="text/javascript">
$(document).ready(function () {
map = {};
$("[id*=txtSearch]").autocomplete({
source: function (request, response) {
$.ajax({
url: '<%=ResolveUrl("~/Search_VB.ashx" ) %>',
dataType: "json",
data: 'prefixText: "' + request.term + '"',
success: function (data) {
items = [];
$.each(data, function (i, item) {
var code = item.Code;
var shortDesc = item.ShortDesc;
map[shortDesc] = { Code: code, ShortDesc: shortDesc };
items.push(shortDesc);
});
response(items);
}
});
},
select: function (event, ui) {
var selectedvalue = ui.item.value;
var shortDesc = map[selectedvalue].ShortDesc;
var code = map[selectedvalue].Code;
$("#TxtDiagnosis1").val(shortDesc);
$("#code1").val(code);
return false;
},
focus: function (event, ui) {
var selectedvalue = ui.item.value;
var shortDesc = map[selectedvalue].ShortDesc;
$("#TxtDiagnosis1").val(shortDesc);
return false;
}
});
});
</script>
</div>
Search_VB.ashx
<%@ WebHandler Language="VB" Class="Search_VB" %>
Imports System.Web
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Text
Imports System.Collections.Generic
Imports System.Web.Script.Serialization
Public Class Search_VB : Implements IHttpHandler
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim prefixText As String = context.Request.QueryString(0).Split(":"c)(1).Replace("""", "").Trim()
Using conn As New SqlConnection()
conn.ConnectionString = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using cmd As New SqlCommand()
cmd.CommandText = "select ContactName,CustomerId from Customers where ContactName like @SearchText + '%'"
cmd.Parameters.AddWithValue("@SearchText", prefixText)
cmd.Connection = conn
Dim customers As New List(Of Customer)()
conn.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
customers.Add(New Customer() With {.Code = sdr("CustomerId").ToString(), .ShortDesc = sdr("ContactName").ToString()})
End While
End Using
conn.Close()
context.Response.Write(New JavaScriptSerializer().Serialize(customers))
End Using
End Using
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
Public Class Customer
Public Property Code() As String
Get
Return m_Code
End Get
Set(value As String)
m_Code = value
End Set
End Property
Private m_Code As String
Public Property ShortDesc() As String
Get
Return m_ShortDesc
End Get
Set(value As String)
m_ShortDesc = value
End Set
End Property
Private m_ShortDesc As String
End Class
End Class
Search_CS.ashx
<%@ WebHandler Language="C#" Class="Search_CS" %>
using System;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
using System.Web.Script.Serialization;
public class Search_CS : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string prefixText = context.Request.QueryString[0].Split(':')[1].Replace("\"", "").Trim();
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select ContactName,CustomerId from Customers where ContactName like @SearchText + '%'";
cmd.Parameters.AddWithValue("@SearchText", prefixText);
cmd.Connection = conn;
List<Customer> customers = new List<Customer>();
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
customers.Add(new Customer() { Code = sdr["CustomerId"].ToString(), ShortDesc = sdr["ContactName"].ToString() });
}
}
conn.Close();
JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue;
context.Response.Write(serializer.Serialize(customers));
}
}
}
public class Customer
{
public string Code { get; set; }
public string ShortDesc { get; set; }
}
public bool IsReusable
{
get { return false; }
}
}
ScreenShot