1

Following scenario: I create a CheckBoxList in my Page_Load (inside (!Page.IsPostBack)), then load some data in my business logic using LINQ. The data produced is a List with different Value and Text properties, which is fine in the moment I call the method (as I checked the return variable with the debugger).

Now as soon as the List object is assigned as the DataSource of my CheckBoxList, using the debugger i realized that the CheckBoxList.Items have overwritten the Item.Value property with the Item.Text property.

So, my desired (and produced) List contains following item, for example: enter image description here

However, after DataBind() of my CheckBoxList, this is what the Items look like in the Items collection: enter image description here

Is there a clean solution for this problem? My further interest is to compare the Items (found by value) with another list of items (actually the SELECTED items; i thought that giving the CheckBoxList a List of ListItems with the Item.Selected property would be the cleanest way to do it.. nope).

Thanks in advance for any suggestions!

5
  • 1
    Are you setting the DataValueField and DataTextField properties of your CheckBoxList so it knows which fields/properties in your datasource to map to the value and text properties? Commented Sep 22, 2014 at 12:46
  • The ListItem's Text returns the Value if it is null. The same applies to the Value property which returns the Text if it's null. stackoverflow.com/questions/15396385/… In this case it seems that there's only the Text. So you should provide the code instead of images, then we can try to fix it. Commented Sep 22, 2014 at 12:49
  • Ben Robinson: no I am not, since I thought that the List<ListItem> would be mapped to match Text/Text and Value/Value (which would make sense, right?). The problem is that these properties are strings, so how can I map the DataValueField to match the Value property of my ListItem, and same for DataTextField / Text property? Tim Schmelter: I did stumble upon that similar question, but in my case it is just vice versa - value gets overwritten by text. I can totally provide my code, but I had the feeling this was more of a logical mistake. Thank you both so far! Commented Sep 22, 2014 at 13:07
  • 1
    @konrad_pe: as mentioned, the Value is overwritten by Text if the Value is null and the Text is not and vice versa. Commented Sep 22, 2014 at 13:35
  • @Tim Schmelter: alright, I get it now. Sorry for being slow on that one. Commented Sep 22, 2014 at 13:41

1 Answer 1

1

Workaround, not really solution: instead of giving my CheckBoxList a List as DataSource I provide a DataTable object as DataSource:

DataTable dt = new DataTable();
dt.Columns.Add("Text");
dt.Columns.Add("Value");

Instead of creating ListItem objects I create new DataRow objects:

DataRow dr = dt.NewRow();
dr["Text"] = "myText";
dr["Value"] = 0001; 
dt.Rows.Add(dr);

Finally, I assign the DataTable to my CheckBoxList. The DataValueField and DataTextField strings have to be the same as the column names of the DataTable:

myCheckBoxList.DataSource = dt;
myCheckBoxList.DataValueField = "Value";
myCheckBoxList.DataTextField= "Text";
myCheckBoxList.DataBind();

In a further step I loop through my CheckBoxList.Items and compare these items with a list of values, setting the ListItem.Selected to true, but since this was not part of the question, I am leaving this behind for now. Please just ask if you need that snippet.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.