473,326 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

how to determine the index that caused the selectedindexchanged event to fire

I have seen a million forums ask this question. This is what has been working for me.

Expand|Select|Wrap|Line Numbers
  1. protected void checkboxlist_SelectedIndexChanged(object sender, EventArgs e)
  2. {
  3.         CheckBoxList list = (CheckBoxList)sender;
  4.         string[] control = Request.Form.Get("__EVENTTARGET").Split('$');
  5.         int idx = control.Length - 1;
  6.         string sel = list.Items[Int32.Parse(control[idx])].Value;  
  7.  
Let me know if I'm way off and have just been lucking out, but I haven't seen a case where the index wasn't the last value in a '$' terminated string.
Aug 4 '10 #1
6 17041
Frinavale
9,735 Expert Mod 8TB
I have no idea what you are doing or why?

-Frinny
Aug 4 '10 #2
Well do you know any other way to get the index of the checkbox that caused the postback in the checkboxlist?
Aug 4 '10 #3
Frinavale
9,735 Expert Mod 8TB
I haven't used the autopostback for the CheckBoxList control very much I guess because I never noticed that the "SelectedIndexChanged" event only gives you a value for the CheckBoxList's SelectedItem property if the checkbox that was changed was "selected" (as opposed to unselected...in this case the SelectedItem is set to some selected check box in the list). I guess this makes sense but it would make a lot more sense if Microsoft added an event called something like "CheckBoxValueChanged" which would let you determine which element was checked/unchecked. So disappointing but this lack of foresight on their behalf makes life interesting.


I approached the problem a little differently...I used JavaScript to set a HiddenField with the id of the checkbox element that was changed by the user.

In the SelectedIndexChanged event I grab the index of the checkbox that was changed from the ID (which is the last number in the ID for the checkbox) and display it on the screen.

Here is my ASP.NET code:
Expand|Select|Wrap|Line Numbers
  1. <asp:Label ID="CheckBoxListPrompt" runat="server" Text="CheckBoxList2:"></asp:Label>
  2. <asp:CheckBoxList ID="MyCheckboxList" runat="server" AutoPostBack="true">
  3.     <asp:ListItem Text="1" Value="1"></asp:ListItem>
  4.     <asp:ListItem Text="2" Value="2"></asp:ListItem>
  5.     <asp:ListItem Text="3" Value="3"></asp:ListItem>
  6.     <asp:ListItem Text="4" Value="4"></asp:ListItem>
  7.     <asp:ListItem Text="5" Value="5"></asp:ListItem>
  8.     <asp:ListItem Text="6" Value="6"></asp:ListItem>
  9.     <asp:ListItem Text="7" Value="7"></asp:ListItem>
  10.     <asp:ListItem Text="8" Value="8"></asp:ListItem>
  11.     <asp:ListItem Text="9" Value="9"></asp:ListItem>
  12.     <asp:ListItem Text="10" Value="10"></asp:ListItem>
  13.     <asp:ListItem Text="11" Value="11"></asp:ListItem>
  14.     <asp:ListItem Text="12" Value="12"></asp:ListItem>
  15.     <asp:ListItem Text="13" Value="13"></asp:ListItem>
  16.     <asp:ListItem Text="14" Value="14"></asp:ListItem>
  17.     <asp:ListItem Text="15" Value="15"></asp:ListItem>
  18.     <asp:ListItem Text="16" Value="16"></asp:ListItem>
  19.     <asp:ListItem Text="17" Value="17"></asp:ListItem>
  20.     <asp:ListItem Text="18" Value="18"></asp:ListItem>
  21.     <asp:ListItem Text="19" Value="19"></asp:ListItem>
  22. </asp:CheckBoxList>
  23. <asp:Label ID="checkBoxSelectedIndex" runat="server" ></asp:Label>
  24. <script type="text/javascript">
  25.     function setChangedCheckbox(element) {
  26.         var hiddenField = document.getElementById('<%=changedCheckBoxID.ClientID %>');
  27.         if (hiddenField) {
  28.             hiddenField.value = element.id;
  29.         }
  30.     }
  31. </script>
  32. <asp:HiddenField ID="changedCheckBoxID" runat="server" />
  33.  

Then in my server-side code (VB.NET) I handle the Page PreRender event and I set each item in the CheckBoxList to call the JavaScript method when it is clicked. In the SelectedIndexChanged event I retrieve the index from the id of the checkbox that was changed:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
  2.   For Each item As ListItem In MyCheckboxList.Items
  3.     item.Attributes.Add("onclick", "setChangedCheckbox(this)")
  4.   Next
  5. End Sub
  6.  
  7. Private Sub MyCheckboxList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyCheckboxList.SelectedIndexChanged
  8.   'checkBoxSelectedIndex.Text = CheckBoxList1.SelectedIndex.ToString
  9.  
  10.   Dim regex As New Regex("[0-9]*$")
  11.   checkBoxSelectedIndex.Text = regex.Match(changedCheckBoxID.Value).Value
  12. End Sub


-Frinny
Aug 4 '10 #4
Acutally the SelectedIndex property in the SelectedIndexChanged event isn't even the checkbox item that caused the event, it's just the first checkbox item that is selected.

In order to find the index of the actual checkbox item that caused the event to fire you either have to compare ViewState from the previous post, use javascript, or read the __EVENTTARGET string.
Aug 4 '10 #5
Thank you very much. This just helped me soooo much. One of my clients wanted a multiselect combo box. There isn't any. So I told them about check box lists and they liked it. I thought I was screwed until I found your post.

@Rocky4Q
Aug 18 '11 #6
Excellent code, I have looking to use the checkboxlist in the same way as the radio button, reason why is because in the future, there will be multiple selections.

I also checked more than 50 code snapshots and yours was the only one that correctly address the issue. Very good. Thanks!!!
Aug 2 '16 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: sandman | last post by:
I've got a combo box that has an event handler set for SelectedIndexChanged event. The problem is that it's firing at startup when I load data from the Form_OnLoad event. I tried setting a flag...
0
by: john | last post by:
I have a dropdown with a SelectedIndexChanged event handler. I want the event to only get called if the dropdown list was the control that caused the postback. But if the value of the list is...
1
by: Jack | last post by:
Hello, I have a dropdown list on a user control and the AutoPostBack property is set to True. I want to use the SelectedIndexChanged event to populate some text boxes based on what the user...
0
by: Tand35006 | last post by:
Hi, I hope some one can help with this. I have a basic webform with 2 DropDownLists and a single DataGrid. What I am trying to do is populate the first DDList from a dataset on Form_Load. I then...
3
by: n. Smith | last post by:
Hi All, Is it normal that the ListView fires the selectedinexchange event twice? I have a LvLoaners list view item that updates 3 text boxes (code below), when I click on an item. I have set...
2
by: Chris Davoli | last post by:
I have a problem where my SelectedIndexChanged on a drop down will fire sporadically. When it won't fire, I go into Debug and set a break point on the SelectedIndexChanged event, it will then fire...
3
by: martin1 | last post by:
Hi, All, I want user select first item (called All) in listbox, then all other items are selected by SetSelected method, but in loop (see code below) whenever going to SetSelected(), the...
11
by: J055 | last post by:
Hi I have a dropdown control which is constructed in another dropdown control SelectedIndexChanged event protected void ddlParamType_SelectedIndexChanged(object sender, EventArgs e) { //...
6
by: tbrown | last post by:
I have a combobox with items like this: {one,two,three}. The selected index is 0, so "one" appears in the combobox text. When the user drops down the list, and selects "two", for example, I...
3
by: | last post by:
Hi all, I have a CheckBoxList control which has about 10 items. I have set autopostback=true and also set an eventhandler for OnSelectedIndexChanged. The problem is I want to identify which...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.