I have a column in a SQL Server 2005 table for a person's race. A CheckBoxList is used to get this information from the user. Since a person can be multi-racial, they can check multiple checkboxes. The checked values are saved as a comma-delimited string to the database.
I have another GUI used by someone who can edit a person's information. An ObjectDataSourc e is used to populate a FormView control. The itemtemplate shows the comma-delimited string, e.g., 'African American, White'. In edit mode I want to show a CheckBoxList with all the possible values and set the Selected property of items matching what has come down from the database.
What is the best way to do this? I can't seem to get a reference to the CheckBoxList using the FormView's FindControl method and I'm not sure which event to use--I tried ItemCreated, DataBound, and PageLoad.
Once I know which event to use, I was going to do something like the following:
Expand|Select|Wrap|Line Numbers
- Child c = (Child)HttpContext.Current.Session["ChildInfo"];
- if (c != null)
- {
- string[] tempArray = c.Race.Split(',');
- CheckBoxList cbl = viewDemog.FindControl("cblRace");
- for (int i = 0; i < cbl.Items.Count; i++)
- for (int j = 0; j < tempArray.Length; j++)
- if (cbl.Items[i].Text == tempArray[j])
- cbl.Items[i].Selected = true;
- }
GM