title: Validate AutoCompleteExtender
body: <p>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.</p>
  
  <p>I'm using ajaxtoolkit (version 20229) on asp.net 2.0.</p>
  
  <p>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.</p>
  
  <p>Do you have any idea?
  Thanks</p>
  
  <p>/<strong><em>*</em>***</strong> Javascript code</p>
  
  <pre><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();
  
            }
  </code></pre>
  
  <p>/<strong><em>*</em>**<em>*</em></strong>*CODEBEHIND code used to populate the autocompletion list</p>
  
  <pre><code>[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
      public static string[] getStrada(string prefixText, int count, string contextKey)
      {
          System.Collections.Generic.List&lt;string&gt; items = new System.Collections.Generic.List&lt;string&gt;();
          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();
      }
  </code></pre>