Use Value from Textbox1 to Autocomplete Textbox2 not working. I want to use the value from Textbox1 to autocomplete Textbox2 i.e.
using (SqlCommand cmd = new SqlCommand("select id,posino from job_cylinder where posino LIKE '%'+@SearchText1+'%' AND pid='"+txtSearch.Text' ", con))
the pid is the value from txtSearch.
My Code Below
<link href="http://code.jquery.com/ui/1.11.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script type="text/javascript">
$(function () {
SearchText();
});
function SearchText() {
$(".autosuggest").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Casc.aspx/GetAutoCompleteData",
data: "{'pid':'" + 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("Error");
}
});
},
select: function (event, ui) {
if (ui.item.val == -1) {
return false;
}
$('#lblUserId').text(ui.item.val);
}
});
}
$(function () {
SearchText1();
});
function SearchText1() {
$(".autosuggest1").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Casc.aspx/GetAutoCompleteData1",
data: "{'posino':'" + 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("Error");
}
});
},
select: function (event, ui) {
if (ui.item.val == -1) {
return false;
}
$('#lblUserId').text(ui.item.val);
}
});
}
</script>
<div>
<asp:TextBox ID="txtSearch" class="autosuggest" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="txtSearch1" class="autosuggest1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</div>
protected void Page_Load(object sender, EventArgs e)
{
Session["posii"] = txtSearch.Text;
}
[WebMethod]
public static List<string> GetAutoCompleteData(string pid)
{
List<string> result = new List<string>();
using (SqlConnection con = new SqlConnection("Data Source=NERO-SIGBENU\\SQLEXPRESS01;Integrated Security=true;Initial Catalog=arpackaging;"))
{
using (SqlCommand cmd = new SqlCommand("select id,pid from job where pid LIKE '%'+@SearchText+'%'", con))
{
con.Open();
cmd.Parameters.AddWithValue("@SearchText", pid);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(string.Format("{0}/{1}", dr["pid"], dr["id"]));
}
return result;
}
}
}
[WebMethod]
public static List<string> GetAutoCompleteData1(string posino)
{
List<string> result = new List<string>();
using (SqlConnection con = new SqlConnection("Data Source=NERO-SIGBENU\\SQLEXPRESS01;Integrated Security=true;Initial Catalog=arpackaging;"))
{
using (SqlCommand cmd = new SqlCommand("select id,posino from job_cylinder where posino LIKE '%'+@SearchText1+'%' AND pid=@pid' ", con))
{
con.Open();
cmd.Parameters.AddWithValue("@SearchText1", posino);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(string.Format("{0}/{1}", dr["posino"], dr["id"]));
}
return result;
}
}
}