Link to home
Start Free TrialLog in
Avatar of Rodrigo Munera
Rodrigo MuneraFlag for United States of America

asked on

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

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.
ASKER CERTIFIED SOLUTION
Avatar of duttcom
duttcom
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rodrigo Munera

ASKER

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.