I'm trying to get checkbox list values to a single string. in ASP.net using C#.
Here's my code.. I have declared this globally.
string hobbies = "";
This is the code to get the selected items to a string.
protected void chkListHobbies_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i=0;i<chkListHobbies.Items.Count;i++)
{
if (chkListHobbies.Items[i].Selected)
{
hobbies += chkListHobbies.Items[i].Value + ",";
}
}
hobbies = hobbies.TrimEnd(',');
I'm displaying this on button click
protected void btnEnter_Click(object sender, EventArgs e)
{
Response.Write("Hobbies= "+hobbies);
}
It doesn't give the expected output. Would like to get this corrected if it's wrong or would like to know how to do it properly. Thanks in advance.