特林设置组合框的选择时,未将对象引用设置到对象的实例 [英] Object reference not set to an instance of an object when tring to set the selection of a combo box

查看:226
本文介绍了特林设置组合框的选择时,未将对象引用设置到对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框 radcombobox控件是这样的:

 < ASP:文本框ID =txt_inner_emp_num=服务器WIDTH =60像素
ontextchanged =txt_inner_emp_num_TextChanged的AutoPostBack =真>< / ASP:文本框>
< Telerik的:radcombobox控件ID =rad_ddl_inner_emp_name=服务器的CausesValidation =FALSE
CollapseDelay =0文化=AR-EGExpandDelay =0过滤器=包含ItemsPerRequest =100
MarkFirstMatch =真WIDTH =380pxEnableAutomaticLoadOnDemand =真EmptyMessage = - 员工的名字 - ShowMoreResultsBox =真的AutoPostBack =真>
< / Telerik的:radcombobox控件>


根据<一href=\"http://demos.telerik.com/aspnet-ajax/combobox/examples/populatingwithdata/autocompletesql/defaultcs.aspx\"相对=nofollow> Telerik的文档


  

设置数据源到radcombobox控件。二者必选其一的DataSourceID或
  DataSource属性要做到这一点,并设置DataTextField和
  DataValueField属性以在数据源中的相应字段中。
  (请注意,使用数据源时,必须设置该属性上的每个
  回传,最方便的Page_Init。)设置
  EnableAutomaticLoadOnDemand为true。



 保护无效BindEmployees()
        {            rad_ddl_inner_emp_name.Items.Clear();
            rad_ddl_inner_emp_name.DataSource = Utilities.GetAllEmployees();
            rad_ddl_inner_emp_name.DataTextField =名;
            rad_ddl_inner_emp_name.DataValueField =emp_num;
            rad_ddl_inner_emp_name.DataBind();        }


 保护无效Page_Init(对象发件人,EventArgs的发送)
        {
            BindEmployees();
        }


 保护无效txt_inner_emp_num_TextChanged(对象发件人,EventArgs的发送)
        {
            rad_ddl_inner_emp_name.ClearSelection();
            。rad_ddl_inner_emp_name.Items.FindItemByValue(txt_inner_emp_num.Text.TrimEnd())请选择= TRUE; //获取此异常对象引用不设置到对象的实例。
        }

我觉得 rad_ddl_inner_emp_name.Items.Count = 0 !之前设置的选择!如何解决这个问题呢?


解决方案

正如我敢肯定你知道现在,通过客户端交互的radcombox预输入功能的文本搜索而不是价值,这就是为什么你不能找到值。

我的建议是具有二级对象由emp_num搜索(假设是,将永远被输入到文本框中的值)。

例如,创建一个全局变量:

 私人字典&LT;字符串,字符串&GT; Emp_Dict =新词典&LT;字符串,字符串&GT;();

然后填充这个字典,当你做你的绑定。下面code假定返回一个IEnumerable类型。如果没有,你可能要填充字典不同。另外,对于这个工作,你必须包括(System.Linq的)。

  VAR数据源= Utilities.GetAllEmployees();
    Emp_Dict = dataSource.ToDictionary(EX =&GT; ex.emp_num,EX =&GT; ex.name);
    rad_ddl_inner_emp_name.Items.Clear();
    rad_ddl_inner_emp_name.DataSource =数据源;
    rad_ddl_inner_emp_name.DataTextField =名;
    rad_ddl_inner_emp_name.DataValueField =emp_num;
    rad_ddl_inner_emp_name.DataBind();

所以,现在我们需要使用的文本改变事件的字典。

 保护无效txt_inner_emp_num_TextChanged(对象发件人,EventArgs的发送)
{
    rad_ddl_inner_emp_name.ClearSelection();
    如果(Emp_Dict.ContainsKey(txt_inner_emp_num.Text.TrimEnd()))
    {
        rad_ddl_inner_emp_name.SelectedValue = txt_inner_emp_num.Text.TrimEnd();
        rad_ddl_inner_emp_name.Text = Emp_Dict [txt_inner_emp_num.Text.TrimEnd()];
    }}

现在时,在文本框中的文本变化,当输入到文本框的有效emp_num的radcombobox控件将更新。

I have a text box and a RadComboBox like this :

<asp:TextBox ID="txt_inner_emp_num" runat="server" Width="60px" 
ontextchanged="txt_inner_emp_num_TextChanged" AutoPostBack="true"></asp:TextBox>
<telerik:RadComboBox ID="rad_ddl_inner_emp_name" runat="server" CausesValidation="False"
CollapseDelay="0" Culture="ar-EG" ExpandDelay="0" Filter="Contains" ItemsPerRequest="100"
MarkFirstMatch="true" Width="380px" EnableAutomaticLoadOnDemand="True" EmptyMessage="-emp name-" ShowMoreResultsBox="True" AutoPostBack="True">
</telerik:RadComboBox>


According to the Telerik Documentation

Set a data source to the RadComboBox. Use either DataSourceID or the DataSource property to do this and set the DataTextField and DataValueField properties to the respective fields in the data source. (Note that when using DataSource you must set the property on each postback, most conveniently in Page_Init.) Set EnableAutomaticLoadOnDemand to true.


 protected void BindEmployees()
        {

            rad_ddl_inner_emp_name.Items.Clear();
            rad_ddl_inner_emp_name.DataSource = Utilities.GetAllEmployees();
            rad_ddl_inner_emp_name.DataTextField = "name";
            rad_ddl_inner_emp_name.DataValueField = "emp_num";
            rad_ddl_inner_emp_name.DataBind();

        }


 protected void Page_Init(object sender, EventArgs e)
        {
            BindEmployees();
        }


 protected void txt_inner_emp_num_TextChanged(object sender, EventArgs e)
        {
            rad_ddl_inner_emp_name.ClearSelection();
            rad_ddl_inner_emp_name.Items.FindItemByValue(txt_inner_emp_num.Text.TrimEnd()).Selected = true;//Get exception here Object reference not set to an instance of an object.
        }

I find rad_ddl_inner_emp_name.Items.Count = 0 !! before set the selection ! How to fix this problem ?

解决方案

As I'm sure you aware of by now, the radcombox typeahead functionality searches text via client side interaction and not by value, which is why you can't find the values.

What I would suggest is having a secondary object to search by emp_num (assuming that's the value that will always be entered into the textbox).

For example, create a global variable:

private Dictionary<string, string> Emp_Dict = new Dictionary<string, string>(); 

Then populate this dictionary when you do your binding. The following code assumes an ienumerable type being returned. If not you may have to populate the dictionary differently. Also, for this to work, you have to include (System.Linq).

    var dataSource = Utilities.GetAllEmployees();
    Emp_Dict = dataSource.ToDictionary(ex => ex.emp_num, ex => ex.name);
    rad_ddl_inner_emp_name.Items.Clear();
    rad_ddl_inner_emp_name.DataSource = dataSource;
    rad_ddl_inner_emp_name.DataTextField = "name";
    rad_ddl_inner_emp_name.DataValueField = "emp_num";
    rad_ddl_inner_emp_name.DataBind();

So now we need to use the dictionary on the text changed event.

protected void txt_inner_emp_num_TextChanged(object sender, EventArgs e)
{
    rad_ddl_inner_emp_name.ClearSelection();
    if (Emp_Dict.ContainsKey(txt_inner_emp_num.Text.TrimEnd()))
    {
        rad_ddl_inner_emp_name.SelectedValue = txt_inner_emp_num.Text.TrimEnd();
        rad_ddl_inner_emp_name.Text = Emp_Dict[txt_inner_emp_num.Text.TrimEnd()];
    }

}

Now when the text changes in the text box, the radcombobox will update when a valid emp_num is entered into the textbox.

这篇关于特林设置组合框的选择时,未将对象引用设置到对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆