Click here to Skip to main content
12,506,338 members (54,195 online)
Rate this:
 
Please Sign up or sign in to vote.
hi
i am using auto complete JS for to textboxs.for the first textbox its working properly but when i used to do the same code for the another texbox its not working. please check my code is below

<script type="text/javascript">
        $(document).ready(function () {
            SearchText();
        });
        function SearchText() {
            $(".autosuggest").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "Default.aspx/GetAutoCompleteData",
                        data: "{'username':'" + document.getElementById('txtSearch').value + "'}",
                        dataType: "json",
                        success: function (data) {
                            response(data.d);
                        },
                        error: function (result) {
                            alert("Error");
                        }
                    });
                }
            });
        }
	</script>
    <script type="text/javascript">
        $(document).ready(function () {
            SearchText();
        });
        function SearchText() {
            $(".autosuggest1").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "Default.aspx/GetAutoCompleteData1",
                        data: "{'username1':'" + document.getElementById('txtSearch2').value + "'}",
                        dataType: "json",
                        success: function (data) {
                            response(data.d);
                        },
                        error: function (result) {
                            alert("Error");
                        }
                    });
                }
            });
        }
	</script>


<div class="span12">
                                              
                                                <input  id="txtSearch2"  class="autosuggest1 span5" style="height: 30px;" type="text" placeholder="Photography Style,Occasion,Category">
                                                <input  style="height: 30px;" type="text" id="txtSearch" class="autosuggest span5" placeholder="Location: City">
                                                <button type="submit" style="margin-top: -11px;" class="btn btn-warning btn-large">
                                                     Search </button>
                                               
                                            </div>

C# code is
[WebMethod]
 
   public static List<string> GetAutoCompleteData(string username)
   {
       List<string> result = new List<string>();
       using (SqlConnection con = new SqlConnection("Data Source=192.168.10.120;Initial Catalog=XXXX;User ID=XXX;password=XXX"))
       {
           using (SqlCommand cmd = new SqlCommand("select top 10 city from tbl_City where city LIKE '%'+@SearchText+'%'", con))
           {
               con.Open();
               cmd.Parameters.AddWithValue("@SearchText", username);
               SqlDataReader dr = cmd.ExecuteReader();
               while (dr.Read())
               {
                   result.Add(dr["city"].ToString());
               }
               return result;
           }
       }
   }
    [WebMethod]
   public static List<string> GetAutoCompleteData1(string username1)
   {
       List<string> result = new List<string>();
       using (SqlConnection con = new SqlConnection("Data Source=192.168.10.120;Initial Catalog=XXXX;User ID=XXX;password=XXX"))
       {
           using (SqlCommand cmd = new SqlCommand("select top 10 Category from Category where city LIKE '%'+@SearchText+'%'", con))
           {
               con.Open();
               cmd.Parameters.AddWithValue("@SearchText", username1);
               SqlDataReader dr = cmd.ExecuteReader();
               while (dr.Read())
               {
                   result.Add(dr["city"].ToString());
               }
               return result;
           }
       }
   }
Posted 4-Aug-14 21:25pm
Updated 4-Aug-14 21:27pm
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

probably two $(document).ready will not work in same page. Make it as one and change any one of the function name and call both functions from single document.ready and get rid of another one. And return it as JSON string from WebMethod using stringfy



Hope this will work for you
  Permalink  
v3
Comments
Rajeev Mehta 19-Aug-14 0:00am
   
Can you please give me a little example
Sreejesh T J Nair 19-Aug-14 1:27am
   
<script type="text/javascript">
$(document).ready(function () {
SearchText();
Search();
});
function SearchText() {
$(".autosuggest").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/GetAutoCompleteData",
data: "{'username':'" + document.getElementById('txtSearch').value + "'}",
dataType: "json",
success: function (data) {
response(data.d);
},
error: function (result) {
alert("Error");
}
});
}
});
}
function Search() {
$(".autosuggest1").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/GetAutoCompleteData1",
data: "{'username1':'" + document.getElementById('txtSearch2').value + "'}",
dataType: "json",
success: function (data) {
response(data.d);
},
error: function (result) {
alert("Error");
}
});
}
});
}
</script>
Rajeev Mehta 19-Aug-14 1:41am
   
Ohk got it thanks for the reply :)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160927.1 | Last Updated 14 Aug 2014
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100