2

I am facing a problem while setting the context key from the client side using jquery and javascript. It is unable to find the function set_contextKey of AutoCompleteExtender of ASP.Net.

Here is my HTML for textbox and AutoCompleteExtender...

<asp:TextBox ID="txtProduct" runat="server" AutoPostBack="true" OnTextChanged="txtProduct_TextChanged" Width="181px" /><ajaxToolkit:AutoCompleteExtender
    ID="AutoCompleteExtender_txtProduct" BehaviorID="acExt" runat="server" TargetControlID="txtProduct"
    CompletionListCssClass="autocomplete_completionListElement" CompletionListItemCssClass="autocomplete_listItem"
    CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem" MinimumPrefixLength="2"
    CompletionInterval="1000" ServicePath="~/WS/Service.svc" ServiceMethod="GetProductsByName"
    EnableCaching="true" DelimiterCharacters=";" UseContextKey="true" OnClientItemSelected="txtProduct_ClientItemSelected">
</ajaxToolkit:AutoCompleteExtender>

and the jquery on change of a dropdownlist is:

function ddlStore_onchange() {
    $('#acExt').set_contextKey($('#<%= ddlStore.ClientID %>').val());
}

It is throwing the error on set_contextKey function. Can anybody explain what I am doing wrong here...

Naveed Butt
  • 2,861
  • 6
  • 32
  • 55

2 Answers2

3

I found out that replacing the line

$('#acExt').set_contextKey($('#<%= ddlStore.ClientID %>').val());

with this line

$find('acExt').set_contextKey($('#<%= ddlStore.ClientID %>').val());

resolved the issue. Don't know why exactly, can anybody help on this ?

Naveed Butt
  • 2,861
  • 6
  • 32
  • 55
  • `$find` is of AJAX and it returns the whole component rather than a simple `Javascript` or `jquery` object, that is why, it is able to find the method `set_contextKey` using `$find` – Naveed Butt Apr 07 '11 at 19:48
  • The problem here is the # character, in jquery is used to make a reference to the id of the object. When you use the $find you dont need the # characer because is not jquery, just the BehaviorID used or defined in your auto complete control. – Juan Apr 22 '15 at 01:46
1

So there are really 2 problems for why the first didn't work:

  1. Using $find() from asp.net ajax toolkit extends objects and adds method properties like .set_contextKey. Using jquery selectors on the same objects will not work.

  2. In addition, the #acExt selector implies client-side id tag "acExt". asp.net (< 4.0) will add a long prefix to the actual client side IDs.

If you want to select asp.net server tags in jQuery, use the property/endswith selector $([id$='serverID']) instead.

Cosmin
  • 21,216
  • 5
  • 45
  • 60
amb
  • 11
  • 1