Checklist box -Please Help

sameera

Member
Joined
Jun 16, 2005
Messages
20
Programming Experience
Beginner
Hi all,


I have a checklistbox in my ASP.net page i want to get only the checked items from that. can anyone help me

I'm using code behined and i think sometimes because of that page get refreshed everytime when i clicked the button

And tell me how to avoid refreshing the page if possible

please help

thankx

sameera
 
You need to iterate through each item in the checklistbox and see if it is selected. If so you could add them to, for example, and arraylist.

VB.NET:
Dim checkedItems As New ArrayList
Dim i As Short
		For i = 0 To cblTest.Items.Count - 1
			If cblTest.Items(i).Selected Then
			    checkedItems.Add(cblTest.Items(i).Text)
			End If
		Next

Blokz
 
Thanx But the problem is still there

Tankx Blokz I tried your method but failed. What I'm doing is i have a seperate module which accept the check list box as a control and selecting the selected item of it.

But the problem is after i pass the checkbox to the function that check box get cleared so that i cant get the checked values


it is something like

public function checkAnswer(cblTest as checklistbox)as boolean
Dim i As Short
For i = 0 To cblTest.Items.Count - 1
If cblTest.Items(i).Selected Then
checkedItems.Add(cblTest.Items(i).Text)
End If
Next


end function
checkAnswer=true

can you help me on this,

And thankx Kulrom for you as well

Thankx and regards,
sameera
 
In your function your return value is boolean? Are you only wanting to know if there are selected values in the CheckBoxList? If so then you should Return True inside your loop. You currently return your boolean value after End Function, which of course won't be seen.

If your intention is to return the checked values from the CheckBoxList, change the return type of your function to Arraylist and return the arraylist that holds the checked values from the function.

For example:

VB.NET:
Public Function checkAnswer(ByVal cblTest As CheckBoxList) As ArrayList
        Dim checkedItems As New ArrayList
        Dim i As Short
        For i = 0 To cblTest.Items.Count - 1
            If cblTest.Items(i).Selected Then
                checkedItems.Add(cblTest.Items(i).Text)
            End If
        Next
        Return checkedItems
    End Function
This way you have an arraylist of only the checked values.

HTH,

Blokz
 
Back
Top