Introduction
In this blog we will
be using the Web Method directly in the code behind.
Objective
- Writing the WebMethod in Code behind.
- Understanding AJAX Auto Complete Extender.
Required
- TargetControlID :- It will hold the ID of the textbox that will be used for auto-complete
- MinimumPrefixLength :- The count of characters required to enter by the user to fetch data from the database matching the Character.
- ServiceMethod :- The name of the webmethod that we have used to get data from database in the web service.(Note :- Name of the web method should be case sensitive).
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server" DelimiterCharacters=""
Enabled="True" ServiceMethod="GetListofCountries" MinimumPrefixLength="1" EnableCaching="true"
ServicePath="" TargetControlID="TextBox1">
</asp:AutoCompleteExtender>
</div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</form>
</body>
</html>
Code Behined
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[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;
}
}
}

dhoni kholiPosted Dec 18, 2019, 9:25 AM
Nothing happen when i type in textbox
samuel henkPosted Jun 18, 2017, 10:16 PM
How to select from the list and get the country ID?
pictorPosted Feb 12, 2014, 12:12 AM
nothing happens when i made entry in textbox
pictorPosted Feb 12, 2014, 12:11 AM
nothin
sourav mondal 0Posted Dec 1, 2013, 1:20 PM
http://dotnetawesome.blogspot.in/2013/12/autocomplete-multi-value-textbox-without-webservice.html
Khargesh RajputPosted Oct 26, 2013, 4:28 AM
WhichType Of problem You Have?
Sachin KhamkarPosted Oct 26, 2013, 3:35 AM
I am not able to hit my service