Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i want to call a autocomplete webservice from javascript from a content page.webservice is in the same web app itself.if i use a simple .aspx page it calls the webservice and everything works fine.but as soon as i apply master page to it,it doesnt work at all.i have not placed script manager on master page.so i want to use script manager on content page itself.
i have googled for it and applied the following

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
XML
<asp:ScriptManager ID="ScriptManager1" runat="server">
   </asp:ScriptManager>

C#
<script type="text/javascript">
     $(document).ready(function () {
         SearchText();
     });
     function SearchText() {
         $("#txtSearch").autocomplete({
             source: function (request, response) {
                 $.ajax({
                     type: "POST",
                     contentType: "application/json; charset=utf-8",
                     url: "AutoComplete.asmx/GetAutoCompleteData",
                     data: "{'username':'" + extractLast(request.term) + "'}",
                     dataType: "json",
                     success: function (data) {
                         response(data.d);
                     },
                     error: function (result) {
                         alert("Error");
                     }
                 });
             },
             focus: function () {
                 // prevent value inserted on focus
                 return false;
             },
             select: function (event, ui) {
                 var terms = split(this.value);
                 // remove the current input
                 terms.pop();
                 // add the selected item
                 terms.push(ui.item.value);
                 // add placeholder to get the comma-and-space at the end
                 terms.push("");
                 this.value = terms.join(", ");
                 return false;
             }
         });
         $("#txtSearch").bind("keydown", function (event) {
             if (event.keyCode === $.ui.keyCode.TAB &&
                        $(this).data("autocomplete").menu.active) {
                 event.preventDefault();
             }
         })
         function split(val) {
             return val.split(/,\s*/);
         }
         function extractLast(term) {
             return split(term).pop();
         }
     }</script>
<div class="ui-widget"><label for="tbAuto">Enter UserName: </label>
<asp:TextBox ID="txtSearch" runat="server" Width="300px">
</div>
Posted

1 solution

Hi when you use master page control then server control of childpage need to be identified by its CLientID

so in stead of using id of control in javascript file better that you should use that control's clientID.

C#
$("#<%txtSearch.ClientID%>").bind("keydown", function (event) {
            if (event.keyCode === $.ui.keyCode.TAB &&
                       $(this).data("autocomplete").menu.active) {
                event.preventDefault();
            }
        })
 
Share this answer
 
Comments
pwavell 3-Jan-14 4:37am    
still doesnt work..whats the problem with using master page?
BK 4 code 3-Jan-14 4:47am    
ohh i forgott to put client id in searchtext function

$("#<%txtSearch.ClientID%>").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "AutoComplete.asmx/GetAutoCompleteData",
data: "{'username':'" + extractLast(request.term) + "'}",
dataType: "json",
success: function (data) {
response(data.d);
},
error: function (result) {
alert("Error");
}
});
},

did you do this ?
pwavell 3-Jan-14 5:11am    
yes..still not working
BK 4 code 3-Jan-14 5:14am    
YOUR SERVICE AND WEBPAGE AND MASTERPAGE ARE IN SAME ROOT FOLDER ?
pwavell 3-Jan-14 6:03am    
yes

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900