Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wanna autopopulate textbox by typing 3letters...how to do this?
please help me out...
Posted

Add ajax autocomplete to text box
C#
<asp:autocompleteextender id="AutoCompleteExtender1" xmlns:asp="#unknown">
                          runat="server"
                          TargetControlID="txtCity"
                          MinimumPrefixLength="3"
                          EnableCaching="true"
                          CompletionSetCount="1"
                          CompletionInterval="1000"
                          ServiceMethod="GetCity"
</asp:autocompleteextender>

write this code in code behind page
C#
[System.Web.Script.Services.ScriptMethod()]
  [System.Web.Services.WebMethod]
  public static List<string> GetListofCountries(string prefixText)
  {
      using (SqlConnection sqlconn = new SqlConnection("Data Source=.;Initial Catalog=hotel;Integrated Security=True"))
      {
          sqlconn.Open();
          SqlCommand cmd = new SqlCommand("select Country from country where Country like '" + prefixText + "%' ", sqlconn);
          cmd.Parameters.AddWithValue("@Country", prefixText);
          SqlDataAdapter da = new SqlDataAdapter(cmd);
          DataTable dt = new DataTable();
          da.Fill(dt);
          List<string> CountryNames = new List<string>();
          for (int i = 0; i < dt.Rows.Count; i++)
          {
              CountryNames.Add(dt.Rows[i]["country"].ToString());
          }
          return CountryNames;
      }
  }

write a code to get data to auto complete.
 
Share this answer
 
v2
Comments
Member 10276220 11-Apr-14 6:22am    
thanx navi_s_Patil...but i did not use autocompleteextender...i did it by using json & javascript...how to do this in json & javascript???
please help me
navi_s_Patil 11-Apr-14 6:28am    
you need to give minLength= 3 for ex
$('#mycomplete')
.autocomplete({
source: url,
minLength: 3,
select: function (event, ui) {
$(this).val(ui.item.label);
$('#myhidden').val(ui.item.value);
return false;
}
})
Member 10276220 11-Apr-14 6:29am    
i did it...but its not working...
this is my code:
<script type="text/javascript">
$(function () {
debugger;
minLength: 3

$("#<%=txtsearch.ClientID%>").autocomplete({
source: function (request, response) {
debugger;
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Search.aspx/GetAutoCompleteSearch",
data: "{'MedicineName':'" + document.getElementById('<%=txtsearch.ClientID%>').value + "'}",
dataType: "json",
success: function (data) {
response(data.d);
},
error: function (result) {
alert("Error");
}
});
}

});

})
</script>
navi_s_Patil 11-Apr-14 6:33am    
hi you need add inside the autocomplete function
Member 10276220 11-Apr-14 6:39am    
hi..I changed it...but still its not working
this is my code:
<script type="text/javascript">
$(function () {
debugger;


$("#<%=txtsearch.ClientID%>").autocomplete({
source: function (request, response) {
debugger;
$.ajax({
minLength: 3,
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Search.aspx/GetAutoCompleteSearch",
data: "{'MedicineName':'" + document.getElementById('<%=txtsearch.ClientID%>').value + "'}",
dataType: "json",
success: function (data) {
response(data.d);
},
error: function (result) {
alert("Error");
}
});
}

});

})
</script>
<script type="text/javascript">
$(function() {
    $("#<%=txtsearch.ClientID%>").autocomplete({
        source: function(request, response) {
            $.ajax({
                url: "Search.aspx/GetAutoCompleteSearch",
                data: "{'MedicineName':'" + document.getElementById('<%=txtsearch.ClientID%>').value + "'}",
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (data) { response(data.d); }, error: function (result) { alert("Error"); }
                    }))
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        },
        minLength: 2
    });
});
</script>

TRY THIS YOU HAVE PLACE MIN LENGTH IN SIDE AUTOCOMPLETE FUNCTION NOT IN AJAX.
I THINK THIS WILL WORK.:)
 
Share this answer
 
v2

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