We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: ASP.NET Web Forms |
Control: AutoComplete |
Published Date: January 27, 2015 |
Last Revised Date: May 8, 2019 |
Product Version: 12.4.0.24
Tags: autocomplete

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.

2X faster development

The ultimate ASP.NET Web Forms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Abhishek Shandilya
Dec 03, 2018

How can i select multiple values programmatically. This solution works only for single item selection.

Reply

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon