Solved

ASP.NET 2005, IE 6 - CheckBox List - SelectedIndex Property Doesn't Return The Actually Selected Itme!

Posted on 2007-12-03
10
682 Views
Last Modified: 2013-11-26
Experts,
I have got a check box list that has 5 items. If I uncheck the 3rd item, in my code, I get the selected index / selected text properties of the first item and not the selected one (3rd item)

How do I capture the unchecked item?

0
Comment
Question by:feesu
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 37

Expert Comment

by:samtran0331
ID: 20395426
you need to loop through the items in the checkboxlist and check the "selected" property
0
 
LVL 37

Expert Comment

by:samtran0331
ID: 20395457
like:
        For Each item As ListItem In CheckBoxList1.Items

            If item.Selected Then

                Response.Write("Selected text: " & item.Text & ", selected value: " & item.Value & "<br />")

            Else

                Response.Write("Not selected text: " & item.Text & ", not selected value: " & item.Value & "<br />")

            End If

        Next

Open in new window

0
 
LVL 14

Expert Comment

by:Dustin Hopkins
ID: 20395468
In asp.net vb it will look something like this:
Protected Sub CheckBoxList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBoxList1.SelectedIndexChanged

        Dim c As ListItem

        For Each c In CheckBoxList1.Items

            If c.Selected = True Then

                Response.Write(c.Value)

            End If

        Next

    End Sub

Open in new window

0
 
LVL 1

Expert Comment

by:dabrat
ID: 20395550
Hello, just for my own knowledge, how would you do the above code examples in C#?

I know it is not required I am just curious.  Thanks~
0
 
LVL 14

Expert Comment

by:Dustin Hopkins
ID: 20395578
The one on top is Mine and the one on the bottom is samtran's

//Mine

protected void CheckBoxList1_SelectedIndexChanged(object sender, System.EventArgs e) 

{ 

    ListItem c; 

    foreach ( c in CheckBoxList1.Items) { 

        if (c.Selected == true) { 

            Response.Write(c.Value); 

        } 

    } 

} 
 

//Samtran's
 
 

{ 

    foreach (ListItem item in CheckBoxList1.Items) { 

        if (item.Selected) { 

            Response.Write("Selected text: " + item.Text + ", selected value: " + item.Value + "<br />"); 

        } 

        else { 

            Response.Write("Not selected text: " + item.Text + ", not selected value: " + item.Value + "<br />"); 

        } 

    } 

} 

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 37

Expert Comment

by:samtran0331
ID: 20395639
For future reference, there are tools available to convert VB.Net to C# and back.
http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx
0
 
LVL 1

Expert Comment

by:dabrat
ID: 20395718
Thank you dusion: and samtran0331, I appreciate the knowledge I learned today!!
0
 

Author Comment

by:feesu
ID: 20398288
Experts,
I did the following but got the below error:

        For Each item As ListItem In Me.chkSelected.Items
            If Not item.Selected Then
                Me.chkSelected.Items.Remove(item)
            End If
        Next

Error:

System.InvalidOperationException was unhandled by user code
  Message="Collection was modified; enumeration operation may not execute."
  Source="mscorlib"
  StackTrace:
       at System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext()
       at FAQ_Question_Edit.chkSelected_SelectedIndexChanged(Object sender, EventArgs e) in C:\Elvis\wwwroot\NIC\DesktopModules\IT\FAQ_Question_Edit.aspx.vb:line 114
       at System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e)
       at System.Web.UI.WebControls.CheckBoxList.RaisePostDataChangedEvent()
       at System.Web.UI.WebControls.CheckBoxList.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent()
       at System.Web.UI.Page.RaiseChangedEvents()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
0
 
LVL 1

Accepted Solution

by:
dabrat earned 125 total points
ID: 20398574
Try this instead -

For i As Integer = 0 To chkSelected.Items.Count - 1
     ' Remove any not selected item...
    Dim listItem As ListItem = chkSelected.Items(i)
   
   If Not listItem.Selected Then
         chkSelected.Items.Remove(listItem)
    End If
 Next

That works for me, not sure why it doesn't work in a For Each
0
 

Author Comment

by:feesu
ID: 20401167
Hi dabrat,
You need to add i = i + 1 inside your If condition to avoid an error when an item is removed.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Json and asp.net mvc 2 43
Authentication of Web Services 3 46
Syntax error 9 45
SSL https .net web site force redirect 3 31
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now