Click here to Skip to main content
15,894,720 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

JavaScript
<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>



HTML
<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
C#
[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
Updated 4-Aug-14 21:27pm
v2

1 solution

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
 
Share this answer
 
v3
Comments
@p@richit 19-Aug-14 0:00am    
Can you please give me a little example
[no name] 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>
@p@richit 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)



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