• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

Using AutoComplete Ajax Control With Separate ID Field from Name Field ...

AutoComplete Extender offers a convenient way to select values froma TextBox. One problem is that multiple details can be displayed, but the whole text is selected by default. I wanted to allow multiple AutoCompletes, select the id without the other details and append the appropriate delimeter in between entries. First, I setup the WebService method. Note that the Stock object has Symbol and Name properties:


   [WebMethod]
    public string[] GetStocksInPrefix(string prefixText, int count)
    {
        int curCount = 0;
        if (count == 0)
        {
            count = 10;
        }
        List<string> items = new List<string>();
        JavaScriptSerializer jss = new JavaScriptSerializer();
        string[] stocks = null;
        string connectionString = getConnectionString("ConnectionString");
        SqlDataReader rdr = null;
        SqlConnection conn = new SqlConnection(connectionString);
        System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand("sproc_aspnet_GetStocksByPrefix", conn);
        try
        {
            command.CommandType = System.Data.CommandType.StoredProcedure;
            conn.Open();
            command.Parameters.AddWithValue("@Prefix", prefixText+"%");
            rdr = command.ExecuteReader();
            string tmp = string.Empty;
            Stock _stock = null;
            while (rdr.Read() && curCount<count )
            {
                _stock = new Stock(rdr["Symbol"].ToString(), rdr["Name"].ToString(), rdr["Exchange"].ToString());
                tmp = rdr["Symbol"] + " " + rdr["Name"];
                items.Add(AutoCompleteExtender.CreateAutoCompleteItem(tmp, jss.Serialize(_stock)));
                curCount++;
            }
            command.Parameters.Clear();
        }
        catch (Exception ex)
        {
        }
        finally
        {
            if (rdr != null) rdr.Close();
            if(conn!=null) conn.Close();
        }
        return items.ToArray();
    }

 

Then, I add the javascript event, OnSymbolSelected,to the OnClientItemSelected event in the AutoComplete Extender:


                    <asp:TextBox ID="txtSymbols" runat="server" Width="300px"></asp:TextBox>
                    <cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
                        TargetControlID="txtSymbols" ServiceMethod="GetStocksInPrefix"
                        ServicePath="~/WebServiceAutoCompleteSymbol.asmx" MinimumPrefixLength="2"
                        CompletionListHighlightedItemCssClass="watermark" CompletionSetCount="10"
                        DelimiterCharacters=", "
                        CompletionListElementID="Symbol"
                        CompletionListItemCssClass="watermarkMatch"                       
                        EnableCaching="true" CompletionInterval="1000" OnClientItemSelected="OnSymbolSelected"
                         >
                    </cc1:AutoCompleteExtender>

Finally, add the javascript function to the aspx page. This function reads the Stock object into results, takes the SelectedText and gets the original string before the new text was completed by replacing the completed text with an empty string. If it is an additional field (that does not contain or ,, then only the id (symbol in this case) is added. Othewise, the original id plus the delimeter and the new id is added:


<script type="text/javascript" language="javascript">  
    function OnSymbolSelected(source, eventArgs)  
    {  
        var results = eval((  + eventArgs.get_value() + ));  
        if (results.symbol != null)   { 
            var symbols = document.getElementById(<%= txtSymbols.ClientID %>).value;
            var original = symbols.replace((results.symbol+ +results.name),);
            if (original.indexOf( )>0 || original.indexOf(,)>0)
                document.getElementById(<%= txtSymbols.ClientID %>).value = original + ( + results.symbol);
            else 
                document.getElementById(<%= txtSymbols.ClientID %>).value = results.symbol;
            }
    }  
</script> 

 Original Source:
http://techron.blogspot.com/2008/12/using-autocomplete-ajax-control-with.html

AddThis Social Bookmark Button

Posted at 10:42:33 am | Permalink | Posted in Ajax  Tutorials  

Related Stuff

  • MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.

  • Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone

  • TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support

  • ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.

  • Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.

  • Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software

  • PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.

Top Stuff

e-messenger

MSN Web Messenger

eBuddy

ASP.NET Ajax CalendarExtender and Validation

AIM Express

Ajax Tools for ASP.NET Developers



About Ajaxlines

Ajaxlines is a project focused on providing its audience with a database of most of Ajax related articles, resources, tutorials and services from around the world.

Its purpose is to showcase the power of Ajax and to act as a portal to the Ajax development community.


Search


Topics

  • .Net (176)
  • Ajax (112)
  • Ajax Games (10)
  • Articles (95)
  • Bookmarking (35)
  • Calendar (21)
  • Chat (45)
  • ColdFusion (3)
  • CSS (84)
  • Email (23)
  • Facebook (84)
  • Flash (20)
  • Google (54)
  • Html (29)
  • Image (12)
  • International Calls & VOIP (7)
  • Java (58)
  • Javascript (280)
  • jQuery (200)
  • JSON (75)
  • Perl (2)
  • PHP (172)
  • Presentation (19)
  • Python (3)
  • Resources (2)
  • RSS (8)
  • Ruby (32)
  • Storage (4)
  • Toolkits (103)
  • Tutorials (227)
  • UI (11)
  • Utilities (174)
  • Web2.0 (18)
  • XmlHttpRequest (29)
  • YUI (13)

© 2006 www.ajaxlines.com. All Rights Reserved. Powered by IRange