Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-03-19
2
Medium Priority
?
544 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

636 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