无法使用AutoCompleteBox WebForm获取DataValueField

时间:2013-06-28 18:07:12

标签: c# asp.net .net webforms telerik

我有一个radAutoCompleteBox,只能获取Text属性,但我需要ID。

我的AppData.cs方法:

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

我在.aspx文件上的InsertTemplate:

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

我的.aspx文件上的DataSource:

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

我的.cs文件:

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;
    }

所以,我只是 cant 使idCompany工作。它总是返回字符串值“IDCompany”,而不是ID实际值。

有什么想法吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

RadControls for ASP.NET AJAX Documentation开始,您似乎需要处理EntryAdded事件,如下所示:

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.";
}

答案 1 :(得分:0)

看起来,没有像使用RadComboBox那样获取id的简单方法,但是你可以遍历条目并逐个文本地查找。沿着这些方向的东西(未经测试):

  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;
                    }
                }
    }