• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1845
  • Last Modified:

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

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.
EX:
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
0
newjeep19
Asked:
newjeep19
1 Solution
 
informaniacCommented:
0
 
Friman001Commented:
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
0
 
newjeep19Author Commented:
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
newjeep19Author Commented:
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") %>' />
                            </ItemTemplate>                  
                        </asp:TemplateField>
0
 
Friman001Commented:
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">
    </asp:ScriptManager>

<%-- 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
0
 
Friman001Commented:
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!
0
 
newjeep19Author Commented:
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]...........
<body><%--onunload="ReloadParent();"--%>
		<form id="form1" runat="server">
			[b]<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" />[/b]			<div id="toolBar">
				<ul>
.............................................................
<%--			VENDOR --%>     
                        <asp:TemplateField HeaderText="Vendor" ItemStyle-CssClass="w25 gvCust" SortExpression="Vendor">
                            <ItemTemplate>
                                <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" />
                        </asp:TemplateField>

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)]
    [System.ComponentModel.ToolboxItem(false)]
    // 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();
        }

        [WebMethod]
        public string[] VendorNameSearch(string prefixText, int count)
        {
            if(prefixText.Equals("Vendor"))
            {
                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;
                oConnection.Open();

                SqlDataReader drVendors = cmd.ExecuteReader();

                //SqlDataReader drProducts = sqlProductProvider.GetNameList(prefixText);
                while (drVendors.Read())
                {
                    prefixText = drVendors["gtri_vendor"].ToString();
                    break;
                }
                drVendors.Close();
            }
             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
0
 
newjeep19Author Commented:
I was able to fix the Webservice and that seems to work.
0
 
newjeep19Author Commented:
Found my own solution
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now