Solved

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

Posted on 2014-03-19
2
495 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

13 Experts available now in Live!

Get 1:1 Help Now