I'm assuming this is a web control since you are storing values in Session. if this is correct, your 1st problem is CODEvalues = ((CheckBoxList)cntrl).Items.ToString(); the value will be System.Web.WebControls.UI.ListItemCollection not the selected values. since you are looping through controls in a generic fashion you will need to store the control id and the selected values in a dictionary and put the dictionary in session. when you pull the dictionary out of the session you can then find the control based on the key in the dictionary and set the values based on the value in the dictionary. however I would question the overall approach, why are you looping through controls, rather than referencing them explicitly? it make the code more difficult to read and therefor less maintainable. if you want to store series of values in session related to a specific page, create an object which reflects those values, store the object in session and reference the properties. something like this CODE[Serializable] class MyPageDto { public MyPageDto() { CheckBoxListA = new List<string>(); CheckBoxListB = new List<string>(); }
public List<string> CheckBoxListAValues {get; private set;} public List<string> CheckBoxListBValues {get; private set;} .... } then you can use it like this CODEvar dto = new MyPageDto();
PutSelectedValuesIn(dto.CheckBoxListAValues, CheckBoxListA); PutSelectedValuesIn(dto.CheckBoxListBValues, CheckBoxListB);
Session.Add("my page dto", dto);
private static void PutSelectedValuesIn(ICollection<string> collection, ListBaseControl control) { foreach(var item in control.Items) { if(item.Checked == false) continue; collection.Add(item.Value); } } then you can pull the values out CODEvar dto = (MyPageDto)Session["my page dto"]; SetSelectedValue(dto.CheckBoxListAValues, CheckBoxListA); SetSelectedValue(dto.CheckBoxListBValues, CheckBoxListB);
private static void SetSelectedValue(IEnumerable<string> collection, ListBaseControl control) { foreach(var item in collection) foreach(var item in control.Items) { if(item.Value != item) continue; item.Selected = true; } } Jason Meckley Programmer Specialty Bakers, Inc.
FAQ855-7190: Database Connection Management FAQ732-7259: Keeping the UI responsive |
|