?
Solved

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

Posted on 2014-03-19
2
Medium Priority
?
532 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 12

Accepted Solution

by:
duttcom earned 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

770 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