I'm using a Web Service page to make a textbox an "auto complete" control. In order to do that, I'm using a web service.
My code in the web service looks like this:
public string[] ISGetCompletionList(string prefixText)
{
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["CLTDPL"].ConnectionString;
SqlConnection conn = new SqlConnection(connectionString);
List<string> Payers = new List<string>();
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT [PAYER_ID], [PAYER_TYPE] FROM [mos_Payer] WHERE " +
"PAYER_TYPE like '%' + @SearchText + '%' ORDER BY PAYER_TYPE ASC";
cmd.Parameters.AddWithValue("@SearchText", prefixText);
cmd.Connection = conn;
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
Payers.Add(string.Format("{0}|{1}", sdr["PAYER_TYPE"], sdr["PAYER_ID"]));
}
}
conn.Close();
}
return Payers.ToArray();
}
Everything works perfectly. However, now I need to filter the CommandText
by one of the fields. So, I know I need to change the cmd.CommandText
line to something like:
cmd.CommandText = "SELECT [PAYER_ID], [PAYER_TYPE] FROM [mos_Payer] WHERE " +
"PAYER_TYPE like '%' + @SearchText + '%' and PAYER_DATE = " + MyForm.PayerDate +
" ORDER BY PAYER_TYPE ASC";
I'm trying to figure out how I'd reference that field on my aspx page.
The javascript that sends all that info over looks like this:
function SetAutoComplete() {
$("#<%=txtPayer.ClientID %>").autocomplete({
source: function (request, response) {
$.ajax({
url: '<%=ResolveUrl("~/Autocomplete.asmx/ISGetCompletionList") %>',
data: "{ 'prefixText': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item.split('|')[0],
val: item.split('|')[1]
}
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
select: function (e, i) {
$("#<%=hfPayer.ClientID %>").val(i.item.val);
},
minLength: 1
});
};