user5239650
user5239650

Reputation:

JQuery auto complete with asp.net

I'm trying to implement a auto-complete function for a TextBox on a .aspx page. This is what I've tried :

<head runat="server">

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"/>
 <script type="text/javascript"src="//code.jquery.com/jquery-1.10.2.js"> </script>
 <script type="text/javascript" src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

 <script type="text/javascript">
  $(function() {
   var availableTags = [
    "IT",
    "Marketing",
    "HR",
    "Accounting",
    "Management"

];
$( "#TextBox1" ).autocomplete({
  source: availableTags
});
 });
</script>

</head>



<asp:TextBox ID="TextBox1" runat="server" BackColor="White"   ForeColor="Black" ></asp:TextBox>

I don't get any errors or anything but it the autocomplete doesn't show at all. Any suggestions what I'm doing wrong?

Upvotes: 1

Views: 53

Answers (1)

Kyojimaru
Kyojimaru

Reputation: 2724

Maybe it's because on asp.net, your control will generate it's own ID when rendered as HTML, so your script of

$( "#TextBox1" ).autocomplete({
  source: availableTags
});

wouln't find the ID, because it will usually rendered as

<input type="text" id="..._TextBox1"/>

so you could try to change the

<asp:TextBox ID="TextBox1" runat="server" BackColor="White"   ForeColor="Black" ></asp:TextBox>

to

<asp:TextBox ID="TextBox1" runat="server" BackColor="White"   ForeColor="Black" ClientIDMode="Static"></asp:TextBox>

add the ClientIDMode="Static" so the ID will be rendered as it is

Upvotes: 2

Related Questions