3
votes

i have an radAutoCompleteBox and just can get the Text property, but i need the ID.

My AppData.cs method:

public IEnumerable<Company> GetAllCompanies()
    {
        _companyRepository = new CompanyRepository();
        return _companyRepository.GetAll();
    }

My InsertTemplate on .aspx file:

<telerik:RadAutoCompleteBox ID="acCompany" runat="server" EmptyMessage="Empresas..." AllowCustomEntry="False" RenderMode="Lightweight"      DataSourceID="CompanyObjectDataSource"  DataTextField="TradeName"  DataValueField="IDCompany"    />

And my DataSource on .aspx file:

asp:ObjectDataSource ID="CompanyObjectDataSource" runat="server" SelectMethod="GetAllCompanies" TypeName="Apontamento.DataSource"

And my .cs file:

protected void radGrid1_OnInsertCommand(object sender, GridCommandEventArgs e)
    {
        var item = e.Item as GridEditFormItem;
        var company = (item.FindControl("acCompany") as RadAutoCompleteBox).Text;
        var idCompany = (item.FindControl("acCompany") as     RadAutoCompleteBox).DataValueField;
    }

So, i just cant make the idCompany work. It always return the string value "IDCompany", and not the ID real value.

Any ideas?

Thanks!

2

2 Answers

0
votes

From the RadControls for ASP.NET AJAX Documentation, it looks like you need to handle the EntryAdded event, like this:

protected void RadAutoCompleteBox1_EntryAdded(object sender, AutoCompleteEntryEventArgs e)
{
    // Label1 is made up here just for example's sake
    // e.Entry.Text will give you the name selected
    // e.Entry.Value will give you the ID selected
    Label1.Text = e.Entry.Value + " was added.";
}
0
votes

Looks like, there's no easy way to get an id like you can do with RadComboBox, but you can loop over Entries and find item by text. Something along these lines (untested):

  var autoCompleteBox = (item.FindControl("acCompany") as RadAutoCompleteBox);
    int idCompany = 0;
    if (autoCompleteBox !=null)
    {
       foreach (AutoCompleteBoxEntry entry in autoCompleteBox.Entries)
                {
                    if (entry.Text == autoCompleteBox.Text)
                    {
                        idCompany = Convert.ToInt32(entry.Value);
                        break;
                    }
                }
    }