Hi lingers,
Refer below code.
HTML
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/themes/smoothness/jquery-ui.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/jquery-ui.min.js"></script>
<script type="text/javascript">
$(function () {
SearchTextJob();
SearchTextEmboss();
SearchTextDie();
SearchTextCylinder();
});
function SearchTextJob() {
$(".autosuggest").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/GetJobAutoCompleteData",
data: "{ 'searchTerm': '" + request.term + "'}",
dataType: "json",
success: function (data) {
if (data.d.length > 0) {
response($.map(data.d, function (item) {
return {
label: item.split('/')[0],
val: item.split('/')[1]
}
}));
}
else {
response([{ label: 'No Records Found', val: -1 }]);
}
},
error: function (result) {
alert(result.responseText);
}
});
},
select: function (event, ui) {
if (ui.item.val == -1) {
return false;
}
$('[id*=hfjobid]').val(ui.item.val.split(',')[0]);
$('[id*=hfjobembid]').val(ui.item.val.split(',')[1]);
$('[id*=hfdiereferneceno]').val(ui.item.val.split(',')[2]);
}
});
}
function SearchTextEmboss() {
$(".autosuggest31").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/GetEmbossAutoCompleteData",
data: "{ 'searchTerm': '" + request.term + "', embid: '" + $('[id*=hfjobembid]').val() + "'}",
dataType: "json",
success: function (data) {
if (data.d.length > 0) {
response($.map(data.d, function (item) {
return {
label: item.split('/')[0],
val: item.split('/')[1]
}
}));
}
else {
response([{ label: 'No Records Found', val: -1 }]);
}
},
error: function (result) {
alert(result.responseText);
}
});
},
select: function (event, ui) {
if (ui.item.val == -1) {
return false;
}
$('[id*=hfembossid]').val(ui.item.val);
$(event.target).autocomplete("close");
setTimeout(function () {
$(event.target).blur();
});
},
minLength: 0
}).focus(function () {
$(this).autocomplete("search");
});
}
function SearchTextDie() {
$(".autosuggest3").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/GetDieAutoCompleteData",
data: "{ 'searchTerm': '" + request.term + "', diereferneceno: '" + $('[id*=hfdiereferneceno]').val() + "'}",
dataType: "json",
success: function (data) {
if (data.d.length > 0) {
response($.map(data.d, function (item) {
return {
label: item.split('/')[0],
val: item.split('/')[1]
}
}));
}
else {
response([{ label: 'No Records Found', val: -1 }]);
}
},
error: function (result) {
alert(result.responseText);
}
});
},
select: function (event, ui) {
if (ui.item.val == -1) {
return false;
}
$('[id*=hfdieid]').val(ui.item.val);
$(event.target).autocomplete("close");
setTimeout(function () {
$(event.target).blur();
});
},
minLength: 0
}).focus(function () {
$(this).autocomplete("search");
});
}
function SearchTextCylinder() {
$(".autosuggest32").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/GetCylinderAutoCompleteData",
data: "{ 'searchTerm': '" + request.term + "', id: '" + $('[id*=hfjobid]').val() + "'}",
dataType: "json",
success: function (data) {
if (data.d.length > 0) {
response($.map(data.d, function (item) {
return {
label: item.split('/')[0],
val: item.split('/')[1]
}
}));
}
else {
response([{ label: 'No Records Found', val: -1 }]);
}
},
error: function (result) {
alert(result.responseText);
}
});
},
select: function (event, ui) {
if (ui.item.val == -1) {
return false;
}
$('[id*=hfcylinderid]').val(ui.item.val);
$(event.target).autocomplete("close");
setTimeout(function () {
$(event.target).blur();
});
},
minLength: 0
}).focus(function () {
$(this).autocomplete("search");
});
}
</script>
<div>
Job:
<asp:TextBox runat="server" Width="100px" ID="txtSearch1" class="autosuggest"></asp:TextBox>
<asp:HiddenField ID="hfjobid" runat="server" />
<asp:HiddenField ID="hfjobembid" runat="server" />
<asp:HiddenField ID="hfdiereferneceno" runat="server" />
<br />
Emboss:
<asp:TextBox runat="server" Width="100px" ID="TextBox73" class="autosuggest31"></asp:TextBox>
<asp:HiddenField ID="hfembossid" runat="server" />
<br />
Die:
<asp:TextBox runat="server" Width="100px" ID="TextBox24" class="autosuggest3"></asp:TextBox>
<asp:HiddenField ID="hfdieid" runat="server" />
<br />
Cylinder:
<asp:TextBox runat="server" Width="100px" ID="TextBox1" class="autosuggest32"></asp:TextBox>
<asp:HiddenField ID="hfcylinderid" runat="server" />
</div>
Namespaces
using System.Data.SqlClient;
using System.Web.Services;
Code
[WebMethod]
public static List<string> GetJobAutoCompleteData(string searchTerm)
{
List<string> result = new List<string>();
using (SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=test;uid=sa;pwd=pass@123;"))
{
using (SqlCommand cmd = new SqlCommand("SELECT id,pid,embid,diereferneceno FROM job WHERE pid LIKE '%'+@SearchText+'%'", con))
{
con.Open();
cmd.Parameters.AddWithValue("@SearchText", searchTerm);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(string.Format("{0}/{1},{2},{3}", dr["pid"], dr["id"], dr["embid"], dr["diereferneceno"]));
}
return result;
}
}
}
[WebMethod]
public static List<string> GetEmbossAutoCompleteData(string searchTerm, string embid)
{
List<string> result = new List<string>();
using (SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=test;uid=sa;pwd=pass@123;"))
{
using (SqlCommand cmd = new SqlCommand("SELECT id,embid FROM job_emboss WHERE id=@Id", con))
{
con.Open();
cmd.Parameters.AddWithValue("@SearchText", searchTerm);
cmd.Parameters.AddWithValue("@Id", embid);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(string.Format("{0}/{1}", dr["embid"], dr["id"]));
}
return result;
}
}
}
[WebMethod]
public static List<string> GetDieAutoCompleteData(string searchTerm, string diereferneceno)
{
List<string> result = new List<string>();
using (SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=test;uid=sa;pwd=pass@123;"))
{
using (SqlCommand cmd = new SqlCommand("select id,dieno from job_die where id=@Diereferneceno order by id asc", con))
{
con.Open();
cmd.Parameters.AddWithValue("@SearchText", searchTerm);
cmd.Parameters.AddWithValue("@Diereferneceno", diereferneceno);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(string.Format("{0}/{1}", dr["dieno"], dr["id"]));
}
return result;
}
}
}
[WebMethod]
public static List<string> GetCylinderAutoCompleteData(string searchTerm, string id)
{
List<string> result = new List<string>();
using (SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=test;uid=sa;pwd=pass@123;"))
{
using (SqlCommand cmd = new SqlCommand("select id,pid,posino from job_cylinder where pid=@Id", con))
{
con.Open();
cmd.Parameters.AddWithValue("@SearchText", searchTerm);
cmd.Parameters.AddWithValue("@Id", id);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(string.Format("{0}/{1}", dr["posino"], dr["id"]));
}
return result;
}
}
}