i have created autocomplete textbox pulling names from database and is working very well, but the challenge am having is to include images in the autocomplete. here is my code
<div class="col-lg-6" style="">
<asp:ScriptManager ID="ScriptManager" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<div class="input-group" style="">
<asp:TextBox ID="SearchText" runat="server" class="form-control" placeholder="Search..."
Width="100%" />
<asp:HiddenField ID="hfUserName" runat="server" />
<span class="input-group-btn">
<asp:LinkButton ID="btnSearch" runat="server" class="btn btn-twitter" OnClick="btnSearch_Click"
EnableViewState="false" CausesValidation="false"> <i class="fa fa-search"></i></asp:LinkButton>
</span>
</div>
</div>
Script
<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/themes/blitzer/jquery-ui.css"
rel="Stylesheet" type="text/css" />
<script type="text/javascript">
$(function () {
$("[id$=SearchText]").autocomplete({
source: function (request, response) {
$.ajax({
url: '<%=ResolveUrl("~/AutoCompleteLink.aspx/GetNames") %>',
data: "{ 'prefix': '" + 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) {
$("[id$=hfName]").val(i.item.val);
},
minLength: 1
});
});
</script>
CSharp
[WebMethod]
public static string[] GetNames(string prefix)
{
List<string> users = new List<string>();
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select Name from User3 where " + "Name like @SearchText + '%'";
cmd.Parameters.AddWithValue("@SearchText", prefix);
cmd.Connection = conn;
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
users.Add(string.Format("{0}", sdr["Name"]));
}
}
conn.Close();
}
}
return users.ToArray();
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSearch_Click(object sender, EventArgs e)
{
string name = Request.Form[SearchText.UniqueID];
string username = Request.Form[hfUserName.UniqueID];
Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Name: " + name + "\\nID: " + username + "');", true);
}