autocomplete key value pair

Last post 09-17-2008 4:27 AM by webvivekar. 16 replies.

Sort Posts:

  • autocomplete key value pair

    09-24-2007, 4:43 AM
    • Member
      248 point Member
    • shihalv
    • Member since 10-26-2006, 2:24 AM
    • Posts 266

    hi, i noticed there is a fix for autocomplete to support key value..

    however is there a documentation on how to utilizie this feature?

    Alvin Shih
    Software Developer
    MCP MCTS MCPD
  • Re: autocomplete key value pair

    09-24-2007, 12:52 PM
    • Contributor
      2,148 point Contributor
    • Phanatic
    • Member since 10-07-2005, 3:48 AM
    • Redmond , WA
    • Posts 396
    Phani Raj
    http://blogs.msdn.com/PhaniRaj
  • Re: autocomplete key value pair

    09-24-2007, 4:39 PM
    Answer
    • Contributor
      2,610 point Contributor
    • kirtid
    • Member since 11-18-2006, 12:41 AM
    • Redmond
    • Posts 658

    We have tweaked the feature a little to make sure it is easy to use and avoided introducing new properties to AutoComplete. If you would like to pass along additional data to the AutoComplete extender it will now detect it automatically if your webservice returns an array of strings that have been serialized using AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(string, string) helper method. You do not need to set any additional properties if you observe this rule and can extract the value from the list item and use it. If you look at the Developement branch of the Toolkit from the source code tab of Codeplex, you will find in the TestingClient project a sample aspx file that demonstrates the usage: AutoComplete\TextValuePair.aspx. 

    Here is an example of that webservice:

                    [System.Web.Services.WebMethod]
                    [System.Web.Script.Services.ScriptMethod]
                    public static string[] GetCompletionListWithContextAndValues(string prefixText, int count, string contextKey)
                    {
                        System.Collections.Generic.List<string> items = new System.Collections.Generic.List<string>(GetCompletionListWithContext(prefixText, count, contextKey));
                        for (int i = 0; i < items.Count; i++)
                        {
                            items[i] = AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(items[i], i.ToString());
                        }
                        return items.ToArray();
                    }
     
    Kirti Deshpande
    Program Manager, Silverlight and ASP.NET AJAX
    Microsoft

    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Re: autocomplete key value pair

    09-24-2007, 11:28 PM
    • Member
      21 point Member
    • afortaleza
    • Member since 04-17-2006, 12:54 PM
    • Fortaleza, Brazil
    • Posts 10

    kirtid, thank you so much for the update on this much anticipated feature, I'm using it and it's working fine. I have noticed that when I put a asp.net TextBox control in a content page (with a MasterPage), the HTML ID generated for it changes, a prefix like ctl00_ContentID_ is inserted. Is there anything in the javascript library that will allow me to input that prefix instead of me having to hard code it ? Here's what I'm currently doing:  

    function setValue(sender, args) {
       $get('ctl00_MainContent_txtRequerente').value = args.get_value();
    }

     

    thank you
    -- Anderson

  • Re: autocomplete key value pair

    09-25-2007, 2:28 AM
    • Member
      248 point Member
    • shihalv
    • Member since 10-26-2006, 2:24 AM
    • Posts 266

    This is really great stuff! it worked for me too.

    anderson, my approach to that is to build my javascript dynamically...

    if you noticed, I have a property called "SelectedKeyID".  I set this property to the Client ID of the textbox control that I want to use to store the Key value.

    string clientSelectMethodName = "Completer_SelectItem_" + SelectedKeyID;if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), clientSelectMethodName))

    {

    StringBuilder js = new StringBuilder();

    js.Append("<script type=\"text/javascript\" language=\"javascript\">");

    js.Append("function " + clientSelectMethodName + "(source, arguments)");

    js.Append("{");

    js.Append("var keyStore = $get('" + SelectedKeyID + "');");

    js.Append("if ( keyStore != null )");

    js.Append("keyStore.value=arguments.get_value();");

    js.Append("}");

    js.Append("</script>");

    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), clientSelectMethodName, js.ToString());

    }

    Alvin Shih
    Software Developer
    MCP MCTS MCPD
  • Re: autocomplete key value pair

    11-02-2007, 9:59 AM
    • Member
      176 point Member
    • James Evans
    • Member since 06-19-2003, 6:53 AM
    • Florida
    • Posts 118

    Hi -

    This is all very cool and maby I am missing something here - but in our situation all we really need is an event that can be trapped server side when an item is selected in the autocomplete list. We want to populate another list (not an aoutocomplete list - just a standard serverside listbox) basised on what is selected from the autocomplete list. If this was answered and I missed it - I appologise.

     Any help or suggestions appreciated

  • Re: autocomplete key value pair

    11-02-2007, 12:28 PM
    • Member
      400 point Member
    • ersheido
    • Member since 02-25-2003, 8:51 AM
    • Posts 102

    I was able to store the selected value in a control (Label or Textbox), which I can then use on the server-side to take action.  Unfortunately though, I can't figure out how to store the value in a hidden field (i.e. if I set the Label or Textbox to Visible=True, it doesn't make it to the page as a control that's accessible to JS).  I'm kind of stuck at this point as well with having to show the selected value on the screen.

  • Re: autocomplete key value pair

    11-02-2007, 5:25 PM
    • Member
      176 point Member
    • James Evans
    • Member since 06-19-2003, 6:53 AM
    • Florida
    • Posts 118
    Hi ersheido –  I was able to solve my problem – maybe it will help you. First set the OnClientItemSelected property of the AutoCompleteExtender to "IAmSelected". Then enter the following javascript into the codebehind for the page.

     

    var txtClinicSearch;

    var lbUsers;

        function pageLoad() {

            txtClinicSearch = $get("<%=txtSearch.ClientID %>");

            lbUsers = $get("<%=lbxUsers.ClientID %>");

    }

     

    function IAmSelected(source, eventArgs ) {

        txtClinicSearch.OnTextChanged;

        lbUsers.focus();
     

     

    Things to note:
    • I am using master and content pages so the client side names of server controls change – that is why I had to do the $get to get a handle for the controls.
    • The function IamSelected fires the server side OnTextChanged event of the text box that is the target of the AutoCompleteExtender. That server side event populates my other list box with the values I want – works perfect.
     Hope this helps

     

  • Re: autocomplete key value pair

    11-14-2007, 6:02 AM
    • Member
      492 point Member
    • srinivasareddy
    • Member since 05-12-2005, 9:09 AM
    • Singapore
    • Posts 101

    Hi,

    I am able retrieve a value from client side, i had requirement where i need to get value from serverside please help me in this regard.

    Thanks & Regards,
    srini.
  • Re: autocomplete key value pair

    11-14-2007, 6:17 AM
    • Member
      248 point Member
    • shihalv
    • Member since 10-26-2006, 2:24 AM
    • Posts 266

    What I did is to use HiddenField to store the key.

    So I created a javascript function that would populate this HiddenField value from the client event ( when user selects an item from the list )

    then from the server-side I can still access the same HiddenField with the updated value.

    Alvin Shih
    Software Developer
    MCP MCTS MCPD
  • Re: autocomplete key value pair

    11-26-2007, 6:20 PM
    • Member
      115 point Member
    • wph101larrya
    • Member since 06-29-2005, 7:48 PM
    • Tallahassee Florida
    • Posts 31

    So far it has been a royal pain. The supposed documentation link mentioned above...

    http://blogs.msdn.com/phaniraj/archive/2007/06/19/how-to-use-a-key-value-pair-in-your-autocompleteextender.aspx?CommentPosted=true#commentmessage

    either is so old that it doesn't work or they are nuts.

    I have a web page that has several options that a user might want and I use multi-views to deliver these.  In one of the views I have four checkboxes for the user to choose among.  I have four autocomplete instances to complement the four checkboxes.  My calls to the database and my loading the autocomplete with a key/value pair is now working fine (after a couple day searching and trying a bunch of crap).

    Reading the "documentation" I have to write some javascript to intercept the selection. Then to hook it up I am to define the OnClientItemSelected property with the function name I made in the javascript.  Sounds simple - doesn't work, but still simple.

    Next I am to some how (no documentation given) extract the key/value pair and trigger a server-side event. The "documentation" as shown will not cause an event to fire on the client. Then since the event never fires there is no chance of sending anything to the server (even if we knew how to do that.)

    People like me have to search endlessly and read hundreds of blogs and posts trying to find clues to something that should be simply written down.  I never have understood why someone doesn't put up a complete post. I do. Imagine the time that would be saved by the developers if they actually showed us how to use it the way they intended it to be used.  But no, that is far too simple.  No the "norm" is to write simple partial routines of snippet code that doesn't work to give the community a hint of what is supposed to be done.  And then to repeat that flawed process over and over.

    So is anyone up to the task? Can a working example actually be wrtten? Is there anyone out there who can actually demonstrate how to postback a value pair in a single aspx page with codebehind?

    Larry Aultman

    As I said the client side even does not fire as written. If you add the () to the declaration the event fires but naturally the signatures don't match so the call fails.

    Here is what DOES NOT WORK to save you some time...

    <script language="javascript" type="text/jscript">

    var textBoxcoName; //alias to the tbcoName textbox name

    var hiddenTextValue; //alias to the hidden field: hideValue

    var hiddenUserID; //alias to the hidden field: hideUserID

    function pageLoad()

    {

    textBoxcoName = $get("<%=tbcoName.ClientID %>"); //gets the fully resolved name for the textbox control

    }

    function AutoCompleteExtender2_ItemSelected(source, eventArgs )

    {

    alert(
    " Key : "+ eventArgs.get_text() +" Value : "+eventArgs.get_value());hiddenTextValue = $get("<%=hideValue.ClientID %>");

    hiddenTextValue.Value = eventArgs.get_text();

     

    hiddenUserID = $get(
    "<%=hideUserID.ClientID %>");

    hiddenUserID.Value = eventArgs.get_value();

     

    textBoxcoName.OnTextChanged;

    }

    </script>

    ....some html page formatting ...

    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" EnablePageMethods="true"></asp:ScriptManager>

    <asp:HiddenField ID="hideValue" runat="server" />

    <asp:HiddenField ID="hideUserID" runat="server" />

    ... some more html page formatting ...

    <asp:TextBox ID="tbcoName" runat="server" CssClass="txtBx"></asp:TextBox>

    <cc1:AutoCompleteExtender ID="AutoCompleteExtender2" runat="server"

    EnableCaching="true"

    CompletionSetCount="10"

    CompletionListCssClass="autocomplete_completionListElement"

    CompletionListItemCssClass="autocomplete_listItem"

    CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem"

    MinimumPrefixLength="2"

    TargetControlID="tbcoName"

    ServiceMethod="getCompanyNames"

    DelimiterCharacters=""

    OnClientItemSelected="AutoCompleteExtender2_ItemSelected">

    </cc1:AutoCompleteExtender>

     

  • Re: autocomplete key value pair

    11-29-2007, 6:41 PM
    • Member
      2 point Member
    • sntacrz411
    • Member since 09-07-2007, 6:05 PM
    • Posts 2

    wph101larrya,

    I have also been having issues getting the client side functionality to work.  I'm gonna paste my code due to your rant, but I noticed nothing that was really "different" between our 2 examples.

     
     This is the InsertItemTemplate inside of my FormView:

    <InsertItemTemplate>
                <table>
                    <tr>
                        <td class="infolabel">
                            To:
                        </td>
                        <td>
                            <asp:TextBox ID="toTextBox" runat="server" Width="300px" Text='<%# Bind("to") %>'
                                SkinID="CustomTextBox" />
                            <cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="toTextBox"
                                ServicePath="~/MyWebService.asmx" ServiceMethod="GetEmailContacts" MinimumPrefixLength="2"
                                EnableCaching="true" CompletionInterval="1000" FirstRowSelected="true" OnClientItemSelected="AutoCompleteSelected">
                            </cc1:AutoCompleteExtender>
                        </td>
                    </tr>
                    <tr>
                        <td class="infolabel">
                            Subject:
                        </td>
                        <td>
                            <asp:TextBox ID="subjectTextBox" runat="server" Width="300px" Text='<%# Bind("subject") %>'
                                SkinID="CustomTextBox" />
                        </td>
                    </tr>
                    <tr>
                        <td valign="top" class="infolabel">
                            Message:
                        </td>
                        <td>
                            <asp:TextBox ID="contentTextBox" runat="server" Text='<%# Bind("content") %>' Rows="8"
                                Width="300px" TextMode="MultiLine" SkinID="CustomTextBox" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                        </td>
                        <td>
                            <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                                Text="Send" />
                             <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False"
                                CommandName="Cancel" Text="Cancel" />
                        </td>
                    </tr>
                </table>
            </InsertItemTemplate>
    Here is my javascript:

     

        <script type="text/javascript">
            function AutoCompleteSelected(source, eventArgs)
            {
                alert(eventArgs.get_text() + " " + eventArgs.get_value());
            }
        </script>
    

     And here is my Web Service:

     

    public string[] GetEmailContacts(string prefixText, int count)
        {
            string[] OUTPUT = new string[count];
            prefixText = prefixText.ToLower();
            MembershipUserCollection c = (MembershipUserCollection)Membership.GetAllUsers();
            int i = 0;
            do
    	    {
    	        foreach (MembershipUser user in c)
                {
                    ProfileCommon userprofile = (ProfileCommon)ProfileCommon.Create(user.UserName);
                    if (user.UserName.ToLower().Contains(prefixText) || user.Email.ToLower().Contains(prefixText) || userprofile.FirstName.ToLower().Contains(prefixText) || userprofile.LastName.ToLower().Contains(prefixText))
                    {
                        OUTPUT[i] = AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(String.Format("{0} {1} ({2})", userprofile.FirstName, userprofile.LastName, user.Email), userprofile.ID.ToString());
                        i++;
                    }
                }
                break;
    	    } while (i < count);
            
            return OUTPUT;
        }
      


    As you can see the implementation is pretty much the same.  Either way, what got it working for me was simply to download the newest version of the Toolkit and update my assemblies.  I hope this helps.
     

  • Re: autocomplete key value pair

    03-17-2008, 1:09 AM
    • Member
      6 point Member
    • farooqbob
    • Member since 03-17-2008, 5:04 AM
    • Posts 3

    i m using more then 1 control for assign a vlaue my code like this

     

    JS

    <script type="text/javascript">

    var hdnKpiID_1 = '<%=hdnKpiID_1.ClientID%>';

    var hdnKpiID_2 = '<%=hdnKpiID_2.ClientID%>';

    var hdnKpiID_3 = '<%=hdnKpiID_3.ClientID%>';

    function IAmSelected_1( source, eventArgs )

    {

     

    //alert( " Key : "+ eventArgs.get_text() +" Value : "+eventArgs.get_value());

    document.getElementById(hdnKpiID_1).value = eventArgs.get_value();

    // document.getElementById(hdnKpiID_2).value = eventArgs.get_value();

    // document.getElementById(hdnKpiID_3).value = eventArgs.get_value();

    alert(document.getElementById(hdnKpiID_1).value);

    }

    function IAmSelected_2( source, eventArgs )

    {

     

    //alert( " Key : "+ eventArgs.get_text() +" Value : "+eventArgs.get_value());

    document.getElementById(hdnKpiID_2).value = eventArgs.get_value();

    // document.getElementById(hdnKpiID_2).value = eventArgs.get_value();

    // document.getElementById(hdnKpiID_3).value = eventArgs.get_value();

    alert(document.getElementById(hdnKpiID_2).value);

    }

    function IAmSelected_3( source, eventArgs )

    {

     

    //alert( " Key : "+ eventArgs.get_text() +" Value : "+eventArgs.get_value());

    document.getElementById(hdnKpiID_3).value = eventArgs.get_value();

    // document.getElementById(hdnKpiID_2).value = eventArgs.get_value();

    // document.getElementById(hdnKpiID_3).value = eventArgs.get_value();

    alert(document.getElementById(hdnKpiID_3).value);

    }

    </script>

     

     and html

    <cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" minimumPrefixLength="1" ServiceMethod = "AutoCom" TargetControlID="txtKPI1" OnClientItemSelected="IAmSelected_1" ServicePath="AutoCom.asmx" EnableCaching ="true" UseContextKey='true'>

    </cc1:AutoCompleteExtender>

    <cc1:AutoCompleteExtender ID="AutoCompleteExtender4" runat="server" minimumPrefixLength="1" ServiceMethod = "AutoCom" TargetControlID="txtKPI2" OnClientItemSelected="IAmSelected_2" ServicePath="AutoCom.asmx" EnableCaching ="true" UseContextKey='true'>

    </cc1:AutoCompleteExtender>

    <cc1:AutoCompleteExtender ID="AutoCompleteExtender5" runat="server" minimumPrefixLength="1" ServiceMethod = "AutoCom" TargetControlID="txtKPI3" OnClientItemSelected="IAmSelected_3" ServicePath="AutoCom.asmx" EnableCaching ="true" UseContextKey='true'>

    </cc1:AutoCompleteExtender>

    &nbsp; &nbsp;&nbsp; &nbsp;

    <asp:HiddenField ID="hdnKpiID_1" runat="server" />

    <asp:HiddenField ID="hdnKpiID_2" runat="server" />

    <asp:HiddenField ID="hdnKpiID_3" runat="server" />

    when i m changing java script function name IAmSelected name to IAmSelected_1. its not working.

    Any one can help me.

    thnx & regards

    Faq

  • Re: autocomplete key value pair

    03-17-2008, 1:33 AM
    • Member
      6 point Member
    • farooqbob
    • Member since 03-17-2008, 5:04 AM
    • Posts 3

    Hooray!!!!!!!

    i got solution

    Just replace Your ajaxToolkit.dll with new version.

     

    thnx & regards

    Faq

  • Re: autocomplete key value pair

    08-15-2008, 12:52 PM
    • Member
      4 point Member
    • webvivekar
    • Member since 09-05-2005, 11:14 AM
    • Posts 2

    Will this work with Master page? I am using the latest 3.5 dll (AjaxControlToolkit.dll)

    I am able to set the value in the javascript ( alert is showing the value), but during postback the value is empty. Any solution?

Page 1 of 2 (17 items) 1 2 Next >