Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Please can anyone tell me how to implement
VB
Autosuggest / Autocomplete with Ajax v. 2.1.3

with asp.net with sample code or any similar sample with jquery and asp.net.
I dont want to use any DLL.

ref. url:
http://www.brandspankingnew.net/specials/ajax_autosuggest/ajax_autosuggest_autocomplete.html[^]

thanks.
Posted
Updated 28-Mar-11 6:39am
v3
Comments
Dalek Dave 28-Mar-11 12:39pm    
Edited for Readability.

 
Share this answer
 
Comments
Dalek Dave 28-Mar-11 12:40pm    
Good Link
The AutoCompleteExtender control provides you the ability to help the end user find what they might
be looking for when they have to type in search terms within a text box. Like the product Google Suggest
once you start typing characters in the text box, you will get results from a
datastore that matches what you have typed so far.
To establish something similar for yourself, create a new page that contains only a ScriptManager control,
an AutoCompleteExtender control, and a TextBox control. The ASP.NET portion of the page should
appear as presented in :
<br />
<br />
lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="AutoComplete.aspx.cs"
Inherits="AutoComplete" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit"
TagPrefix="cc1" %>
XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>AutoComplete</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
TargetControlID="TextBox1" ServiceMethod="GetCompletionList"
UseContextKey="True">
</cc1:AutoCompleteExtender>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>



Again, like the other ASP.NET AJAX controls, you extend the TextBox control using the TargetControlID
property. When you first add these controls to the page, you will not have the ServiceMethod
property defined in the AutoCompleteExtender control. Using Visual Studio 2008, you can make the
framework for a service method and tie the extender control to this method all from the design surface.
After expanding the TextBox control’s smart tag, select the Add AutoComplete page method option from
the provided menu.
This action creates a service method in the code-behind for your page.
C#
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
public partial class AutoComplete : System.Web.UI.Page
{
[System.Web.Services.WebMethodAttribute(),
System.Web.Script.Services.ScriptMethodAttribute()]
public static string[] GetCompletionList(string prefixText, int count,
string contextKey)
{
SqlConnection conn;
SqlCommand cmd;
string cmdString =
"Select CompanyName from Customers WHERE CompanyName LIKE ’" +
prefixText + "%’";
conn = new
SqlConnection(@"Data Source=.\SQLEXPRESS;
AttachDbFilename=|DataDirectory|\NORTHWND.MDF;
Integrated Security=True;User Instance=True");
// Put this string on one line in your code
cmd = new SqlCommand(cmdString, conn);
conn.Open();
SqlDataReader myReader;
<pre lang="xml">List&lt;string&gt; returnData = new List&lt;string&gt;();
myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (myReader.Read())
{
returnData.Add(myReader[&quot;CompanyName&quot;].ToString());
}
return returnData.ToArray();
}
}
 
Share this answer
 
Comments
Dalek Dave 28-Mar-11 12:40pm    
Comprehensive Answer!
trilokharry 29-Mar-11 5:10am    
thanks for your kind response.
server side method is not being executed.
I used code as mentioned below.
.aspx page
<cc1:ToolkitScriptManager runat="server" ID="ScriptManager2" />
<cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="TextBox1"
ServiceMethod="GetCompletionList" UseContextKey="True">

<asp:TextBox ID="TextBox1" runat="server">

.aspx.cs page:
[WebMethod]
public string[] GetCompletionList(string prefixText, int count, string contextKey)
{
if (count == 0)
{
count = 10;
}

if (prefixText.Equals("xyz"))
{
return new string[0];
}

Random random = new Random();
List<string> items = new List<string>(count);
for (int i = 0; i < count; i++)
{
char c1 = (char)random.Next(65, 90);
char c2 = (char)random.Next(97, 122);
char c3 = (char)random.Next(97, 122);

items.Add(prefixText + c1 + c2 + c3);
}

return items.ToArray();
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900