To Create an AutoComplete TextBox in asp.net use following instructions:
1. Create a webmethod GetDivisionInfo
public string[] GetDivisionInfo(string prefixText)
{
string connStr = ConfigurationManager.ConnectionStrings["DatabaseConnection"].ConnectionString;
int count = 10;
string sql = "Select * from Division Where vDivisionName like @prefixText";
SqlDataAdapter da = new SqlDataAdapter(sql,connStr);
da.SelectCommand.Parameters.Add("@prefixText", SqlDbType.VarChar, 50).Value = prefixText+ "%";
DataTable dt = new DataTable();
da.Fill(dt);
string[] items = new string[dt.Rows.Count];
int i = 0;
foreach (DataRow dr in dt.Rows)
{
items.SetValue(dr["vDivisionName"].ToString(),i);
i++;
}
return items;
}
Replace connStr with your existing connection string.
2. Drag and Drop a TextBox from ToolBar and set the AutoCompleteExtender’s TargetControlID property to the TextBox Id. Set ServicePath as WebService.asmx, ServiceMethod as GetDivisionInfo and MinimimPrefixLength as 1.
<asp:TextBox ID="txtDivision" runat="server"></asp:TextBox>
<cc1:AutoCompleteExtender ID="txtDivision_AutoCompleteExtender" runat="server"
DelimiterCharacters="" Enabled="True" ServicePath="WebService.asmx" ServiceMethod="GetDivisionInfo"
TargetControlID="txtDivision" MinimumPrefixLength="1">
</cc1:AutoCompleteExtender>
Discussion
No comments yet.