Best way to store and populate checkboxlist items to database

//Save the Selected Checkbox list items values by seperated by comma like 1,2,3,5,6…e.t.c in single row , and popluate the checkbox list by uding saved values

//Getting Selected values By comma use bellow sample code

 

publicstring GetSelectedItems(CheckBoxList control)    

{

StringBuilder items =

new StringBuilder();        

foreach (ListItem item in control.Items)        

{

if (item.Selected)                

items.Append(

string.Format(“{0},”, item.Value));        

}

//Remove last comma before returning back.        

return items.ToString().TrimEnd(‘,’);    

}

//Populate Values to checkbox list from database value

privatevoid PopulateHotelSelectedItems(CheckBoxList control, string values)    

{

string[] k = values.Split(‘,’);        

for (int i = 0; i <=k.Length-1; i++)        

{

ListItem currentCheckBox = control.Items.FindByValue(k[i].ToString());            

if (currentCheckBox != null)            

{

currentCheckBox.Selected =

true;            

}

}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s