views:

124

answers:

3

Hi

I want to use autocomplete (jquery or .net) with force a choice so that the user must always choose something. There can be no possibility of entry, the user can only choose.

Does anyone know how to do this??

I want to use webservice for this purpose, because the results will be a lot, and I'll show you the first few.

I want to display text in hidden field after choosing to save the value

A: 

I want to use webservice for this purpose, because the results will be a lot, and I'll show you the first few

dzajdol
You should delete this answer and edit your question to add any additional information.
tvanfosson
A: 

Doing this with the jQuery UI widget is going to require a bit of extra work on your part. I suggest that you consider using the non-UI autocomplete plugin with the mustMatch option. Note, though, that if the user has javascript turned off, this will not guarantee that the value of the input is always from your set of options.

EDIT: Sample parser for the autocomplete plugin. Expects an array of JSON objects of the form:

[ {
    "FirstName" : "john",
    "LastName" : "smith",
    "Email" : "[email protected]"
  },
  ...
]

Autocomplete setup

$('.selector').autocomplete({
      ...
      parse: function(data) {
                var array = new Array();
                for(var i=0;i<data.length;i++)
                {
                    var datum = data[i];
                    var name = datum.FirstName + ' ' + datum.LastName;
                    var display =  name + ' (' + datum.Email + ')';
                    array[array.length] = { data: datum, value: display, result: name };
                }
                return array;
             },
      ...
})
tvanfosson
if it supports the text - value items??I want to display text in hidden field after choosing to save the value
dzajdol
You can basically send back any sort of data by providing a `parse` method and adding a `result` handler. The `parse` method (added as an option) takes the data returned from the server and returns an array of objects with `data`, `value`, and `result` parameters. I'll add a sample implementation.
tvanfosson
sample implementation - that was great :)
dzajdol
A: 

I created a class for JSON responses: /// /// Class reprezented postcode in jquery autocomplete list /// public class PostCodeJson { public String Text { get; private set; } public String Value { get; private set; }

    #region Constructors
    /// <summary>
    /// Empty constructor
    /// </summary>
    public PostCodeJson()
    {
        this.Text = String.Empty;
        this.Value = String.Empty;
    }
    /// <summary>
    /// Constructor
    /// </summary>
    /// <param name="_text"></param>
    /// <param name="_value"></param>
    public PostCodeJson(String _text, String _value)
    {
        this.Text = _text;
        this.Value = _value;
    }
    #endregion Constructors
}

and function returns list of this class using in webservices method: [WebMethod] public List GetPostCodesCompletionListJson(String prefixText, Int32 count) { return LibDataAccess.DBServices.PostCodes.GetPostCodeJson(prefixText, count); }

And in aspx i do this that: $(document).ready(function() { $("#<%=pc.ClientID %>").autocomplete( baseUrl + "WebServices/Autocomplete.asmx/GetPostCodesCompletionListJson", { parse: function(data) { var array = new Array(); for (var i = 0; i < data.length; i++) { var datum = data[i]; var name = datum.Text; var display = name; array[array.length] = { data: datum, value: display, result: datum.Value }; } return array; }, dataType: "xml" }); });

and when you enter something in the box i got an error: Request format is unrecognized for URL unexpectedly ending in '/GetPostCodesCompletionListJson

What am I doing wrong??

dzajdol