简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM