Adding AutoCompleteExtender in Textbox in ASP.NET

Published: Last Updated on 949 views 3 minutes read
A+A-
Reset

Hi, we will learn how to enable AutoCompleteExtender in Textbox Control.

Here, we will learn to Map data from SQL Database to Textbox within Same Page. WebMethod in .cs Page.

AutoComplteExtender code properties

AutoCompleteExtender in ASPX Page Code

<tr>
            <td class="style1">
                Vehicle No :
            </td>
            <td>
                <asp:TextBox ID="txtVehicleNo" runat="server" ToolTip="Enter Vehicle Number (Full)"
                    Width="168px"></asp:TextBox>
                <asp:AutoCompleteExtender ID="txtVehicleNo_AutoCompleteExtender" runat="server" DelimiterCharacters=""
                    Enabled="True" ServiceMethod="GetVehicleList" ServicePath="" TargetControlID="txtVehicleNo"
                    UseContextKey="True" MinimumPrefixLength="2">
                </asp:AutoCompleteExtender>
            </td>
        </tr>

Now Code in ASPX.CS Page

Add these Namespaces for below Code.

using System.Data; // For Dataset
using System.Data.SqlClient; // For SQlConnection/Adaptor
using System.Configuration; // Access ConnectionString from Web.Config.
using System.Collections.Generic;
[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
public static string[] GetVehicleList(string prefixText, int count, string contextKey)
{
  SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConToJohn"].ConnectionString);
  SqlDataAdapter adp = new SqlDataAdapter("Select distinct VehicleNo from VehicleDispatch where VehicleNo like '%" + prefixText + "%'", con);
  DataSet ds = new DataSet();
  adp.Fill(ds, "Vehicles");
  string[] VehicleNo = new string[ds.Tables["Vehicles"].Rows.Count];
  for (int i = 0; i <= ds.Tables["Vehicles"].Rows.Count - 1; i++)
  {
   VehicleNo.SetValue(ds.Tables["Vehicles"].Rows[0][0].ToString(), i);
  }
  return VehicleNo;
}

I have Selected Only Unique Value from Database using SELECT DISTINCT …. command. If you want all records, Simply Change SQL Query to SELECT … Will be glad to hear from you.

Please subscribe to our YouTube channel for more ASP.NET related videos and tutorials.

Related Posts

Leave a Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Index

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.