Articles in this section
Category / Section

How to select an item in AutoComplete from code behind by using the key field ?

3 mins read

Description

AutoComplete control value can be selected by using its key value from code behind.  This enables you to select a unique item when there are multiple items with the same Text value.

Solution

To select the AutoComplete TextBox value from code behind by using its Key value, you can use the SelectValueByKey property. By passing the corresponding Key value in the SelectValueByKey property, the Value is selected in the AutoComplete TextBox accordingly.

ASPX

<ej:Autocomplete ID="AutoComplete1" runat="server" ShowPopupButton="true"/>

 

CodeBehind CS

protected void Page_Load(object sender, EventArgs e)
{
    this.AutoComplete1.DataSource = new AClist().GetDataItems().ToList();
    this.AutoComplete1.DataUniqueKeyField = "ID";
    this.AutoComplete1.DataTextField = "Text";
    this.AutoComplete1.SelectValueByKey = "6"; // new property to select item based on key
    }
public class AClist {
    public AClist() { }
    public string ID { get; set; }
    public string Text { get; set; }
    public List<AClist> GetDataItems()
    {
        List<AClist> data = new List<AClist>();
        data.Add(new AClist { ID = "1", Text = "Action Script" });
        data.Add(new AClist { ID = "2", Text = "C++" });
        data.Add(new AClist { ID = "3", Text = "Java" });
        data.Add(new AClist { ID = "4", Text = "Python" });
        data.Add(new AClist { ID = "5", Text = "C#" });
        data.Add(new AClist { ID = "6", Text = "FoxPro" });
        data.Add(new AClist { ID = "7", Text = "PHP" });
        data.Add(new AClist { ID = "8", Text = "Logo" });
        return data;
    }
}

 

Note:

The SelectValueByKey property can also be set from the ASPX page.



Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied