Solved

How to get SelectedIndex from a submitted checkbox in WebForms C# .net

Posted on 2014-03-19
2
505 Views
Last Modified: 2014-03-20
I have the following input in a form
<input id="ContentPlaceHolder_CBMemberType_3" type="checkbox" name="ctl00$ContentPlaceHolder$CBMemberType$3" value="SEASON">

Open in new window


In my .cs file, to access the selected items, I do a foreach loop like so:

foreach (ListItem item in CBMemberType.Items)

Open in new window


I can add the text associated with the checkbox to a list in the following code:

            {
                if (item.Selected)
                {

                    string Member_Descr = item.Text;

                    if (Member_Descr == "Active Military/Deferral") { Member_Descr = "ACTMIL"; }
                    if (Member_Descr == "Active Member") { Member_Descr = "REGULAR"; }
                    if (Member_Descr == "Season Off") { Member_Descr = "SEASOFF"; }
                    if (Member_Descr == "Seasonal On") { Member_Descr = "SEASON"; }
                    if (Member_Descr == "Service Fee Member") { Member_Descr = "SERVICE_FEE"; }
                    if (Member_Descr == "Special Retired Affiliate") { Member_Descr = "SPEC_RET"; }

                    MemberType = MemberType + "'" + Member_Descr + "',";
                    MemberTypeP = MemberTypeP + Member_Descr + ",";

                    if (itemFilter.Contains("Member Type") == false) { itemFilter.Add("Member Type"); itemFilterID.Add("0"); }
                    itemFilter.Add(item.Text);
             
                    
                }
            }

Open in new window


But I also want to add the SelectedIndex to another list. but I can't access the index using "item.SelectedIndex", when I debug "CBMemberType", I can see an int with the "SelectedIndex" I need but I can't get it in my code. What am I missing? I'm a bit new to C# and web forms so any help would be greatly appreciated.
0
Comment
Question by:Rodrigo Munera
2 Comments
 
LVL 12

Accepted Solution

by:
duttcom earned 500 total points
ID: 39941347
First thought - SelectedIndex is a single value which relates to the list item currently selected e.g. list item 3 (which is why you get an integer), so there aren't multiple values to loop through and add to a list.

Are you trying to get the index of the list items that contain checked checkboxes? If so, this may help you-

http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.checkeditems(v=vs.110).aspx
0
 
LVL 4

Author Closing Comment

by:Rodrigo Munera
ID: 39942777
Thanks! I ended up just creating a counter in each "foreach" loop and iterating through it. It gave me the index in the same order as the item was being passed so that helped me match the selected value to the selected index.

Just in case you're curious, I was dealing with a list of members, and the functionality had to do with how to filter that list based on specific criteria. The list is usually hidden, but the values the list is being filtered by is visible, I wanted the user to be able to remove an item from the list, and the form would automatically submit, so I needed to be able to map the item's ID based on what the current set of filters were applied to the list.  I would've done this differently but I'm working with another developer's code. Someone who is very fond of using <input type="submit" /> buttons for everything (links, headers, labels, divs). But that's another story altogether.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

825 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