We help IT Professionals succeed at work.

ASP.NET Textbox auto-reslove using C# and ASP.NET

newjeep19 asked
I need to know (whith a code example) on how to search against a data base for a list of vendors when the user (auto-reslove) types in part of a name or just one letter in a textbox on a web page to search against a database then list out or show those options.
User types in the letter T and the Asp.Net textbox begains to search for and then rechrives a list of Vendors who start with the letter T. They then can select one of the names and the name is inserted into that same textbox. Also, the textbox is contianed in row in a GridView.
Thank you in advance for your help
Watch Question

Obadiah ChristopherDeveloper User Interface

I think you would want to use a DropDownList.  This will allow you to display all of the values to the user.  Use the <asp:DropDownList> tag!  Then, in the codebehind file, use "DataSource" and "DataBind()" to bind a datasource to that control, displaying all of the Vendors!

Make sense?

Ryan F


OK I tried installing the Ajax solution and it did not work caused errors. I guess that there is not away in C# and or ASP.NET to achive what I am looking to do?
Basically, I have a grideview and in each gridview row I have a testbox for the "Vendor". The goal is when a user types in part of a name of a vendor or even just a letter or two that a list ( can be a ddl with all of the vendors stored in our database will then show on the ddl (dropdownlist). It was suggested to use an Ajax Toolkit and I tried to download one and then add it to VS 2010. When I did that the toolkit did not work.
Thoughts / examples or am I just out of luck....thanks


here is my C# code for the vendor textbox:
mVendor.Attributes.Add("onfocus", String.Format("FocusVendor(this, {0});", lineNumber));
it creates an dynamic popup
ASP.NET code:
<asp:TemplateField HeaderText="Vendor" ItemStyle-CssClass="w25 gvCust" SortExpression="Vendor">
                            <ItemTemplate>                                      <asp:TextBox ID="Vendor" runat="server" Text='<%# Eval("Vendor") %>' />
This really does not seem visable when talking about using a Textbox.  if you used a DropDownList, you could give them a list to select from.

As far as Javascrip goes...

When you use Javascript you need to make sure that you use a script manager.  Also, you need to make sure that you download the jquery library.

<%-- Put this in the <body> tag. -->
<asp:ScriptManager ID="ScriptManager2" runat="server">

<%-- Put this in the <head> tag. -->
<script src="../Scripts/jquery-1.8.0.min.js" type="text/javascript"></script>

Open in new window

This would require you to download the Javascript libraries to your Project directory.  If you load this library before you start coding in Javascript, like in my example, you should be able to make use of all of the functions in that library.

Ryan F
That our make sure you *.dll file is referenced.... if that is what this topic is about, I am just going off my thoughts right now!


OK the ajax tool kit was not installed correctly as a tab to VS2010, I now have it intalled correcttly. However the orginal issue still remains .... when the user clicks into a textbox and the requierments is for a textbox....they need to be able to type in a letter or the begining of a name say...CIS....and the auto reslove will then bring up a list of or attempt to fill out the rest of the name. I added the the following lines of code to my project on the ASP.NET
[b]<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>[/b]...........
		<form id="form1" runat="server">
			[b]<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" />[/b]			<div id="toolBar">
<%--			VENDOR --%>     
                        <asp:TemplateField HeaderText="Vendor" ItemStyle-CssClass="w25 gvCust" SortExpression="Vendor">
                                <asp:TextBox ID="Vendor" runat="server" AutoPostBack="true"  />
                              [b]   <asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="Vendor" ServicePath="~/WebService1.asmx" 
                                     ServiceMethod="VendorNameSearch" MinimumPrefixLength="1" CompletionSetCount="10">
                                 </asp:AutoCompleteExtender>[/b]                            </ItemTemplate>                  
                            <ItemStyle CssClass="w25 gvCust" />

Open in new window

And then I created a WebService.asmx.cs
/// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService {

        public WebService1()
            // InitializeCommponent();

        public string[] VendorNameSearch(string prefixText, int count)
                return new string[0];
            List<string> vendorName = new List<string>(count);
            for (int i = 0; i < count; i++) 
                string connectionString = @"MyCompnaiesdatabase";
                SqlConnection oConnection = new SqlConnection(connectionString);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = oConnection;
                cmd.CommandType = CommandType.Text;

                SqlDataReader drVendors = cmd.ExecuteReader();

                //SqlDataReader drProducts = sqlProductProvider.GetNameList(prefixText);
                while (drVendors.Read())
                    prefixText = drVendors["gtri_vendor"].ToString();
             return vendorName.ToArray(); 

Open in new window

When I type into the textbox......on the ASP.NET form....nothing happends the auto-reslove does not work.....Please help...thanks
I was able to fix the Webservice and that seems to work.


Found my own solution

Explore More ContentExplore courses, solutions, and other research materials related to this topic.