Hiii,
With reference to below link:
https://www.aspforums.net/Threads/458558/Ajax-AutoComplete-Textbox-with-multiple-column-using-C-and-VBNet-in-ASPNet/
After selection anywhere in the row i want to pass values to respective textboxes.
I have 4 controls
txtFirstName,LastName,drpGender,txtMobile
After selecting on row the values should go to respective textboxes and also need to fetch id field value to store in database.
In the below code i want to get keyvalue pair means fetch id of selected item in codebehind to do further operations on onselectchanged event.
Also if i select on any row the selected value set to the autocmplete textbox. there i want to set only empname value.
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<asp:TextBox ID="txtEmployeeSearch" runat="server"></asp:TextBox>
<cc1:AutoCompleteExtender ServiceMethod="SearchEmployees" MinimumPrefixLength="1"
CompletionInterval="100" EnableCaching="false" CompletionSetCount="10" TargetControlID="txtEmployeeSearch"
OnClientPopulated="Employees_Populated" ID="AutoCompleteExtender1" runat="server"
FirstRowSelected="false">
</cc1:AutoCompleteExtender>
</div>
<div>
<script type="text/javascript">
function Employees_Populated(sender, e) {
var employees = sender.get_completionList().childNodes;
var div = "<table>";
div += "<tr><th>EmployeeId</th><th>FirstName</th><th>LastName</th><th>City</th><th>Country</th></tr>";
for (var i = 0; i < employees.length; i++) {
div += "<tr><td>" + employees[i].innerHTML.split('-')[0] + "</td><td>" + employees[i].innerHTML.split('-')[1] + "</td><td>" + employees[i].innerHTML.split('-')[2] + "</td><td>" + employees[i].innerHTML.split('-')[3] + "</td><td>" + employees[i].innerHTML.split('-')[4] + "</td></tr>";
}
div += "</table>";
sender._completionListElement.innerHTML = div;
}
</script>
</div>
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> SearchEmployees(string prefixText, int count)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager
.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT EmployeeId, FirstName, LastName,City,Country FROM Employees WHERE FirstName like @SearchText + '%'";
cmd.Parameters.AddWithValue("@SearchText", prefixText);
cmd.Connection = conn;
conn.Open();
List<string> employees = new List<string>();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
employees.Add(AjaxControlToolkit.AutoCompleteExtender
.CreateAutoCompleteItem(string.Format("{0}-{1}-{2}-{3}-{4}",
sdr["EmployeeId"].ToString(), sdr["FirstName"], sdr["LastName"], sdr["City"], sdr["Country"]),
sdr["EmployeeId"].ToString()));
}
}
conn.Close();
return employees;
}
}
}
<script type = "text/javascript">
function ClientItemSelected(sender, e) {
$get("<%=hfCustomerId.ClientID %>").value = e.get_value();
}
function OnEmployeeSelected(source, eventArgs) {
var idx = source._selectIndex;
var employees = source.get_completionList().childNodes;
var value = employees[idx]._value;
var text = employees[idx].firstChild.nodeValue;
source.get_element().value = text;
}
</script>
Tried both script but wont work.
The main purpose of this to fetch ID from selected item.