开发者

List<>.Find(delegate) issue

开发者 https://www.devze.com 2023-01-04 06:18 出处:网络
I have a multi-column combobox where the datasource is a List<> in my Select Class Select selection = new Select();

I have a multi-column combobox where the datasource is a List<> in my Select Class

Select selection = new Select();  
RadComboBox1.DataSource =  selection.GetAcctUtilCo(e.Text, 10).Skip(e.NumberOfItems); 

I have a few DataTextFields. My DataValueField is the AcctID.

Once an account is selected, I need the datatextfield values to populate some fields in a gridview.

I am trying to use the .Find() method by AcctID to retreive the data without su开发者_如何学编程ccess:(

int AcctID = Convert.ToInt32(RadComboBox1.SelectedValue); // *
List<Select> mylist = RadComboBox1.DataSource as List<Select>;  
mylist.Find(delegate(SelectTop act) { return act.AcctID == acctID; }); // ** exception here
Label lblAcctNo = (Label)grdAccts.HeaderRow.FindControl("lblAcctNo");  
lblAcctNo.Text = mylist.AccountNum;

When I debug, I get 'Object reference not set to the instance of an object' on the indicated line. AcctID is NOT null when I hover over it. However when I hover over mylist, it says null. I'm new with the .Find method & I'm really not sure if the problem is with that or with using the datasource of the combobox as mylist source.

Can someone please help enlighten me


Your combo-box's data source is not a list. When you use "as" the result is null if you try to cast to an invalid type, instead of throwing an exception like a standard cast. Since you used the Skip function to create your data source you actually have an "IEnumerable<>".


The problem is caused by RadComboBox1.DataSource - it is not persisted between page requests.

  1. In your code that sets RadComboBox1.DataSource, save a copy of the RadComboBox1.DataSource value in the viewstate. eg. ViewState["RadComboBox1"] = RadComboBox1.DataSource;

  2. In the event that runs your above code, restore the RadComboBox1.DataSource by reading the value from the viewstate. eg. RadComboBox1.DataSource = ViewState["RadComboBox1"];

You should hopefully find the values then persist between requests. Good luck!

0

精彩评论

暂无评论...
验证码 换一张
取 消