How to get a value of Selected item from ChekboxList

Posted by Syedshakeer under ASP.NET category on | Views : 4046
How to get a value of Selected item from a ChekboxList

1)first drag and drop a CheckBoxList on your webPage.
2)Add Some Items.
3)Drag and drop one Button.

After selecting Your items from Checkboxlist, Click on Button to get values or text selected by you.

protected void Submit_Click(object sender, EventArgs e)
String str=null ;
for (int i = 0; i < CheckBoxList1.Items.Count ; i++)
if (CheckBoxList1.Items[i].Selected == true)
Response.Write (str = CheckBoxList1 .Items [i].Text .ToString ()+"<br>");


Comments or Responses

Posted by: Deeraj on: 5/15/2009 Level:Starter | Status: [Member]
Dear SyedShakeer,

I wonder if this code snippet was tested.

Corrected Code:

foreach (ListItem chk in chkList.Items)
if (chk.Selected == true)

Please do test your code before you post it.

Remember! DotNetFunda is growing and the World is watching you!

Posted by: Syedshakeer on: 5/15/2009 Level:Starter | Status: [Member]
Hi Deeraj
I tested My code before Posting.It is working fine.
Then i Thought the code will be useful for someone.
Thanks for giving commnent on my code.

Posted by: Vuyiswamb on: 5/29/2009 Level:NotApplicable | Status: [Member] [MVP] [Administrator]
The Code if fine, there is nothing wrong with. When you post your Code please use the code Tag so that everything will be formatted well.

Thanks for posting.

Login to post response