Wrox Programmer Forums
|
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
 
Old February 11th, 2004, 04:48 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default CheckBoxList

How do I capture multiple values from a checkboxlist that are submitted?

 
Old February 12th, 2004, 02:46 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

Code:
foreach( ListItem Citem in CheckBoxList1.Items )
{
    if( Citem.Selected )
        <s>// DO WHAT U WANT...</s>
}
HTH.

Always:),
Hovik Melkomian.
 
Old February 12th, 2004, 03:20 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

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.
 
Old February 13th, 2004, 10:10 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

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
 
Old February 13th, 2004, 10:18 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

That worked.

Thank you.

 
Old May 24th, 2006, 04:36 AM
Authorized User
 
Join Date: Jan 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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"

 
Old May 24th, 2006, 10:01 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Something is off. Post your code block here. Can't really tell from just the error.

 
Old May 24th, 2006, 07:55 PM
Authorized User
 
Join Date: Jan 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old May 24th, 2006, 08:35 PM
Authorized User
 
Join Date: Jan 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old May 25th, 2006, 02:50 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

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.







Similar Threads
Thread Thread Starter Forum Replies Last Post
CheckboxList and SQLDataSource keithroby ASP.NET 3.5 Basics 5 December 3rd, 2008 03:19 PM
Checkboxlist epkgupta Visual Studio 2005 0 March 11th, 2008 05:31 AM
CheckBoxList Headers Ric_H C# 0 June 26th, 2006 10:29 AM
Get Values from Checkboxlist derekl ASP.NET 1.0 and 1.1 Basics 3 May 26th, 2006 09:46 AM
CheckBoxList eddiema VS.NET 2002/2003 1 April 9th, 2004 09:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.