I am trying to have autoCompleteExtender to populate on focus of a textbox all possibilities. So far I am having no luck. I have tried adding onfucus in the textbox tag to call a js function that calls the webmethod to populate from a datatable with no luck. The webmethod is being called but nothing shows up on the page. Only after typing something in the textbox does any suggestions turn up. Here is the aspx page control:
<div title="Model" runat="server" style="text-align:left; padding:20px"><strong>Model</strong>
<asp:TextBox ID="tbModel" runat="server" onfocus="ModelTBOnFocus()"></asp:TextBox>
<div id="ModelListPlacement" style="height:100px; overflow-y:scroll;" ></div>
<ajaxToolkit:AutoCompleteExtender ID="tbModel_AutoCompleteExtender" runat="server" DelimiterCharacters=""
Enabled="True" ServiceMethod="GetListofModels" MinimumPrefixLength="1" EnableCaching="true"
ServicePath="" TargetControlID="tbModel" CompletionInterval="50" CompletionSetCount="40"
CompletionListElementID="ModelListPlacement"></ajaxToolkit:AutoCompleteExtender>
</div>
And Here is the CodeBehind webMethod:
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> GetListofModels(string prefixText)
{
using (SqlConnection sqlconn = new SqlConnection(GetConnectionStringValue("")))
{
sqlconn.Open();
SqlCommand cmd = new SqlCommand("SELECT DISTINCT(Model) FROM Assets WHERE Model like '" + prefixText + "%' " + ModelQuery, sqlconn);
cmd.Parameters.AddWithValue("@Model", prefixText);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
List<string> Models = new List<string>();
TextInfo myTI = CultureInfo.CurrentCulture.TextInfo;
for (int i = 0; i < dt.Rows.Count; i++)
{
string model = myTI.ToTitleCase(dt.Rows[i]["Model"].ToString().ToLower());
Models.Add(model);
}
return Models;
}
}
This is working code. I am just trying to get all possibilities to show up when the textbox is focused. Any help would be much appreciated. Thanks!
EDIT: This js is working also, but I am not getting any suggestions until something is typed. Here is the javascript code to call the function:
<script type="text/javascript">
function ModelTBOnFocus() {
PageMethods.GetListofModels("");
}
</script>