This is a migrated thread and some comments may be shown as answers.

Using AjaxControlToolkit Autocomplete and Get Unique Value Pair

3 Answers 500 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chris Lee
Top achievements
Rank 1
Chris Lee asked on 13 Oct 2008, 09:53 AM
Hi,

I am using the AjaxControlToolkit AutoCompleteExtender inside an ASP.NET AJAX RadGrid.  It works fine with the following code:

<script type="text/javascript" language="javascript">
    function GetDogID(source, eventArgs) {
        var DogIDTxt = document.getElementById('<%=DogIDTxt.ClientID%>');
        DogIDTxt.value = eventArgs.get_value();
    }
</script>
<telerik:GridTemplateColumn DataField="trialCode" HeaderText="trialCode"
            SortExpression="trialCode" UniqueName="trialCode">
            <EditItemTemplate>
                <asp:TextBox ID="trialCodeTextBox" runat="server" autocomplete="off" onKeyPress="KeyPress()"></asp:TextBox>
 <cc1:AutoCompleteExtender ID="trialCodeTextBox_AutoCompleteExtender" runat="server"
            DelimiterCharacters="" Enabled="True"
            ServicePath="/assets/webservice/DogList.asmx" TargetControlID="trialCodeTextBox"
            ServiceMethod="GetCompletionList" MinimumPrefixLength="2"
            CompletionSetCount="20" CompletionInterval="0" OnClientItemSelected="GetDogID"
        >
        </cc1:AutoCompleteExtender>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="trialCodeLabel" runat="server" Text='<%# Eval("trialCode") %>'></asp:Label>
            </ItemTemplate>
        </telerik:GridTemplateColumn>

I use a web service to pull a list of values from a database and all is well.

However...

I am using the OnClientItemSelected method (relatively new to AutoCompleteExtender so that I am able to pull back a value pair from the web service: 1.) the text value (which I'm displaying as 'autocomplete' in the RadGrid textbox while in edit mode and 2.) a primary key ID value of the item that i'm placing on a label control somewhere else on the page for now while i'm trying to figure out how to get it inside the RadGrid for use in updating.

What I need to do is be able to submit the update (or add a new record) from the RadGrid and use the primary key value on the page and NOT the text value pulled from the autocomplete.

The user needs to be able to select from a 'friendly' text-based list of about 3,000 items (hence, the need for autocomplete).  Then, once selected, the primary key (from the value pair that was returned from the web service) needs to be the value that the RadGrid inserts when making the update---not the text value as there could be duplicates.

Is this possible? 

Chris

Here's the web service i'm using:

<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
<System.Web.Script.Services.ScriptService()> _
Public Class DogList
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function GetCompletionList(ByVal prefixText As String, ByVal count As Integer) As String()
        Dim connString As String = ConfigurationManager.AppSettings("DataConnect2")
        Dim conn As New SqlConnection(connString)
        Dim mycommand As New SqlCommand
        Dim rs As SqlDataReader
        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 registrationNumber, dogName + ' - #' + registrationNumber As 'TheDog' FROM Dogs WHERE dogName LIKE '" & Replace(prefixText, "'", "''") & "%' ORDER BY dogName 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
                    Dim thevalues As String = AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(rs("TheDog").ToString(), rs("registrationNumber").ToString())
                    items.Add(thevalues)
                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




3 Answers, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 16 Oct 2008, 12:02 PM
Hello Christopher,

For your convenience I prepared a sample project demonstrating how to attain the desired functionality.
AutoComplete extender is applied on the first column in RadGrid.

Please review the project attached to this forum post.

Best regards,
Nikolay
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Chris Lee
Top achievements
Rank 1
answered on 17 Oct 2008, 09:38 AM
Hi Nikolay,

Thank you for providing that project.  It worked perfectly.

Sincerely,

Chris
0
Suzy
Top achievements
Rank 2
answered on 27 Jan 2015, 08:38 AM
Although this post is from 2006, it still helped me a lot!
Had the same issue with a ajax autoextender used in the popup edit form of a radgrid, not giving the key back but the description.
Thanks for this solution
Tags
Grid
Asked by
Chris Lee
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
Chris Lee
Top achievements
Rank 1
Suzy
Top achievements
Rank 2
Share this question
or