views:

55

answers:

2

I am trying to implement the autocomplete method for textboxes.

I would like to use the example based on jquerys autocomplete provided here

<head>
  <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"&gt;&lt;/script&gt;
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"&gt;&lt;/script&gt;

  <script>
  $(document).ready(function() {
    $("input#autocomplete").autocomplete({
    source: ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"]
});
  });
  </script>
</head>

The first and Main problem is that i do not know the way of passing my values to the source:

By saying that i mean, supposing i have a a server-side ASP.NET function GetTheResults as listof(string) or GetTheResults as string ()

how am i supposed to pass those values as source required by the auto-complete?

The second problem is that the specific way does not use AJAX. Which means i guess on the load of the form i will load all the available values to the source. Isn't that an overkill?

What do you suggest instead?

Thanks and regards!

A: 

Hello,

You could instead manually write out the content from the server to the client. Try creating a public method called:

public string GetJs()
{
   var data = this.GetTheResults();
   string output = "[";
   bool comma = false;

   foreach (var i in data)
   {
      if (comma)
        output += ", ";
      else
        comma = true;

       output += '" + i + "'";
   }

   return output + "]";
}

And change your client-side script to look like:

 $(document).ready(function() {
    $("input#autocomplete").autocomplete({
    source: <%= GetJs() %>
});
  });

You may have to inject the <%= %> as a string, and instead do:

source: eval("<%= GetJs() %>")

Or you could write that entire JS from the page to the output.

HTH.

Brian
A: 

Oh, maybe I misunderstood - is GetTheResults a server-side ASP.NET function? You didn't say. This assumes it's a client-side JavaScript function. If it is server-side, why not implement the AJAX way?


If GetTheResults returns all values then I think you can just fill it in inline, i.e.

$("input#autocomplete").autocomplete({
    source: GetTheResults()
    }
});

The control will then filter the list for display. Or you can use the function source mechanism as documented on that page, e.g.

$("input#autocomplete").autocomplete({
    source: function(request, response) {
        var results = GetTheResultsForInput(request.term);
        response(results);
    }
});
Rup
Yeap! GetTheResults is a server-side ASP Function. Sorry for not mentioning that.
Chocol8