Go Back   DeveloperBarn Forums > Programming & Scripting > Code Samples

Sponsored Links

Discuss "Using AJAX Auto Complete Extender with Database" in the Code Samples forum.

Code Samples - Search through our code samples to give your application that something extra or provide a code sample of your own.


Reply « Previous Thread | Next Thread »  
 
LinkBack Thread Tools Display Modes
  #1  
Old March 26th, 2008, 07:59 AM
richyrich's Avatar
Moderator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 395
Thanks: 26
Thanked 32 Times in 32 Posts
Blog Entries: 1
Rep Power: 1
richyrich will become famous soon enough

Awards Showcase
Classic ASP JavaScript 
Total Awards: 2

Default Using AJAX Auto Complete Extender with Database

This coding example is to show how to use an AJAX Auto Complete Extender in ASP.NET to give users dropdown options for a textbox as they type.

autocomplete.aspx - This is your normal aspx page
Code:
<%@Page Language="VB" CodeFile="autocomplete.aspx.vb" Inherits="autocomplete" %> 
<%@Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxtoolkit" %> 
'ajaxcontroltoolkit.dll must be in your bin folder 
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
  <title>Auto Complete Extender</title> 
</head> 
<body> 
  <form id="form1" runat="server">
   'the method used to grab the autocomplete data is contained in a 
   'webservice. We must tell the ScriptManager where to find this file and 'add it as a service 
   'Note: I call my .NET extensions prefix atlas. This is defined in your 'web.config and can be any prefix you want to use (just so you're not 'confused)  
<atlas:ScriptManager ID="ScriptManager1" runat="server">        
 <Services>            
   <atlas:ServiceReference Path="autocomplete.asmx" />        
 </Services> 
</atlas:ScriptManager> 
<div align="center">        
   <asp:TextBox ID="textbox1" runat="server" />        
   'the prefix here is as defined in Registration of the ajax control  
   'toolkit at the top of our page.       
   'Basically, you define what textbox control you want to auto         
   'complete. In this case it's textbox1. You tell it where the service         
   'path can be found (autocomplete.asmx) and what the name of        
   'the method you've created in your web service (in this case         
   'getemails - you'll see this used later)
<ajaxtoolkit:AutoCompleteExtender ID="comp_textbox1" runat="server" TargetControlID="textbox1" ServicePath="autocomplete.asmx" ServiceMethod="getemails" MinimumPrefixLength="2" /> 
</div> 
</form> 
</body> 
</html>
Then we have the web service code behind
autocomplete.vb - This is stored in your App_Code folder. Note the codebehind reference in our autocomplete.asmx file
Code:
 
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports MySql.Data.MySqlClient
' this is only needed because I use MySQL. The Mysql connector must be in your bin folder
 
Imports System.Collections.Generic
' Added to the default web service template as we're using a list to return the auto complete options.
 
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
 
<System.Web.Script.Services.ScriptService()> _
'we have to add this line to enable the service to be called from Javascript
 
Public Class autocomplete_email
Inherits System.Web.Services.WebService
 
<WebMethod()> _
 
'This is where our method starts. Note the name (getemails) is as
'used in the servicemethod when we added the autocomplete 
'extender to our .aspx page
 
'prefixText is the text that the user enters into the textbox
 
Public Function getemails(ByVal prefixText As String, ByVal count As Integer)
 
      Dim connString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("myconnection").ConnectionString
      Dim conn As New MySqlConnection(connString)
      Dim mycommand As New MySqlCommand
      Dim rs As MySqlDataReader
      Dim items As New List(Of String)
 
      'we add the prefixText into our SQL query to retrieve the options 
      'for our autocomplete
      Dim strsql As String = "SELECT email FROM users WHERE email LIKE '" & replace(prefixText,"'","''") & "%' ORDER BY email ASC"
      mycommand.Connection = conn
      mycommand.CommandText = strsql
 
         Try
            conn.Open()
            rs = mycommand.ExecuteReader
            If rs.HasRows Then
 
                    'we loop through the results adding each result to our list of 
                    'items
                        Do While rs.Read
                        items.Add(rs("email"))
                    Loop
 
            End If
 
      Catch ex As Exception
              items.Add(ex.ToString)
              ' this will show the exception in the autocomplete list if 
              ' there are any errors
 
      Finally
              mycommand.Dispose()
              conn.Close()
              conn.Dispose()
 
      End Try
 
        Return items.ToArray ' we return our list of items as an array
End Function
End Class
Hope that helps.

Last edited by lewy; March 26th, 2008 at 10:47 PM. Reason: fixing [code] tags
Reply With Quote
Sponsored Links
Reply

  DeveloperBarn Forums > Programming & Scripting > Code Samples

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
.Net And Ajax smithcarvo .Net Development 2 August 20th, 2008 07:50 AM
Finding Datalist Row on auto postback richyrich .Net Development 1 May 12th, 2008 04:52 AM
Dynamic Content in AJAX Extender richyrich .Net Development 3 March 17th, 2008 12:14 PM


All times are GMT -4. The time now is 03:58 PM.



Content Relevant URLs by vBSEO 3.2.0