|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
February 11th, 2004, 04:48 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
CheckBoxList
How do I capture multiple values from a checkboxlist that are submitted?
|
February 12th, 2004, 02:46 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
|
|
Code:
foreach( ListItem Citem in CheckBoxList1.Items )
{
if( Citem.Selected )
<s>// DO WHAT U WANT...</s>
}
HTH.
Always:),
Hovik Melkomian.
|
February 12th, 2004, 03:20 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
I added this:
Dim Citem As ListItem
Dim myHolder
Dim cblRooms As CheckBoxList = myCatHolder2.FindControl("chkRooms")
For Each Citem In cblRooms.Items
If Citem.Selected Then
myHolder = Citem
End If
Next
dbComm.Parameters("rooms").Value = myHolder.ToString
It still only takes the last value checked. Am I missing something? If I add Response.Write(Citem) it displays all items checked in one long string.
|
February 13th, 2004, 10:10 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
|
|
Stu,
The reason that it is only taking the last item is that each time you are going through your loop you are setting "myHolder" to a new value, not appending it to the current value. Try replacing:
myHolder = Citem
with:
myHolder += Citem
and see if that helps.
You may have to add a "<br>" if you don't want them to all be on the same line.
J
|
February 13th, 2004, 10:18 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
That worked.
Thank you.
|
May 24th, 2006, 04:36 AM
|
Authorized User
|
|
Join Date: Jan 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi, I tried to test your code but when i change myHolder += citem It giving me the following error
Operator '+' is not defined for typed "System.Object" and "System.Web.UI.WebControls.ListItem"
|
May 24th, 2006, 10:01 AM
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
|
|
Something is off. Post your code block here. Can't really tell from just the error.
|
May 24th, 2006, 07:55 PM
|
Authorized User
|
|
Join Date: Jan 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
This is my code to loop the checkboxlist. But I having error at myHolder += citem
Dim Citem As ListItem
Dim myHolder
For Each Citem In cbl_Codes.Items
If Citem.Selected Then
myHolder += Citem
End If
Next
|
May 24th, 2006, 08:35 PM
|
Authorized User
|
|
Join Date: Jan 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
thanks rstelma, now i can retrieve the selected value. But it in one whole line. I understand that katsarosj mention about "<br>". Can i store it in array? or other methods? so i can insert into database easily. Thanks
|
May 25th, 2006, 02:50 PM
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
|
|
You can put them into an array but you will have to loop through the values at some point anyway. Check the code at this post:
http://p2p.wrox.com/topic.asp?TOPIC_ID=44665
As you are looping through the values you can just put them in the database at that time.
|
|
|