0

Hi i need to validate a textbox value in order to accept only values that are in the completion list of the associated autocompleteextender control.

I'm using ajaxtoolkit (version 20229) on asp.net 2.0.

For now i use the code below to validate the textbox ; as you can see i had a hiddenfield that keep the selected key. The hiddenfield is set to 0 if the user enter a value without selecting it from the list.

Do you have any idea? Thanks

/**** Javascript code

   function AutoCompleteItemPopulated(source, eventArgs)
        {
             var assocHiddenField = document.getElementById( source.get_element().id+'_hidden');
             assocHiddenField.value=0;
}



function AutoCompleteItemSelected(source, eventArgs)
        {
            var assocHiddenField = document.getElementById( source.get_element().id+'_hidden');

            assocHiddenField.value = eventArgs.get_value();

          }

/*****CODEBEHIND code used to populate the autocompletion list

[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static string[] getStrada(string prefixText, int count, string contextKey)
    {
        System.Collections.Generic.List<string> items = new System.Collections.Generic.List<string>();
        DataSetIncidentiTableAdapters.StradarioTableAdapter adapter = new DataSetIncidentiTableAdapters.StradarioTableAdapter();
        DataSetIncidenti.StradarioDataTable dtStrade = adapter.GetStrade(contextKey, prefixText);

            foreach (DataSetIncidenti.StradarioRow strada in dtStrade.Rows)
            {
                items.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(strada.DenominazioneCompletaVia, strada.IdStrada.ToString()));
            }

        return items.ToArray();
    }
Liuc
  • 61
  • 3

1 Answers1

0

Yes this can be validated; you need to use a CustomValidator to do this, which you can setup both a client and server validation function, and then check the hidden field for its value.

This works great for us.

HTH.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • Ok, thanks, now the validation works correctly, but after validation error is displayed, the autocompelte doesn't work anymore. – Liuc Mar 02 '11 at 08:20
  • There may need to be some re-initialization, this sounds like a bug, and is something you may want to log on the ACT forum (forums.asp.net). Then again, it may have been fixed in a later version too... – Brian Mains Mar 02 '11 at 13:38