Monday, June 29, 2009

Pre-selecting items from CheckBoxList

I'm not much of an ASP.NET guy, but from time to time I've got to write some simple ASP.NET pages.

I've tried to use data-bound CheckBoxList (data binding done with the built-in wizard), and update the data with some pre-selected values. For some reason CheckBoxList Items are not populated when Page_Load function is called, so the list can't be updated.

Solution A: Found it on Google, but should have thought about it myself - just remove the data-binding done with the VS wizard, and do all the data handling in code.

Solution B (better): Call 'DataBind()' before pre-selecting the CheckBoxList items.

Weird. I must be missing something here. Anyway - it's working.

Tuesday, June 23, 2009

Generics Dictionary<> clone

Here's a simple routine that will clone generic Dictionary:

public static Dictionary<S, V> Clone<S,V>(Dictionary<S, V> dictionary)
Dictionary<S, V> cloneDictionary = new Dictionary<S, V>();
foreach (KeyValuePair<S, V> kvp in dictionary)
cloneDictionary.Add(kvp.Key, kvp.Value);

return cloneDictionary;