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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rocky4Q
    New Member
    • Aug 2010
    • 4

    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.

    Code:
    protected void checkboxlist_SelectedIndexChanged(object sender, EventArgs e)
    {
            CheckBoxList list = (CheckBoxList)sender;
            string[] control = Request.Form.Get("__EVENTTARGET").Split('$');
            int idx = control.Length - 1;
            string sel = list.Items[Int32.Parse(control[idx])].Value;
    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.
    Last edited by Frinavale; Aug 4 '10, 01:34 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    I have no idea what you are doing or why?

    -Frinny

    Comment

    • Rocky4Q
      New Member
      • Aug 2010
      • 4

      #3
      Well do you know any other way to get the index of the checkbox that caused the postback in the checkboxlist?

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        I haven't used the autopostback for the CheckBoxList control very much I guess because I never noticed that the "SelectedIndexC hanged" 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 "CheckBoxValueC hanged" 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 SelectedIndexCh anged 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:
        Code:
        <asp:Label ID="CheckBoxListPrompt" runat="server" Text="CheckBoxList2:"></asp:Label>
        <asp:CheckBoxList ID="MyCheckboxList" runat="server" AutoPostBack="true">
            <asp:ListItem Text="1" Value="1"></asp:ListItem>
            <asp:ListItem Text="2" Value="2"></asp:ListItem>
            <asp:ListItem Text="3" Value="3"></asp:ListItem>
            <asp:ListItem Text="4" Value="4"></asp:ListItem>
            <asp:ListItem Text="5" Value="5"></asp:ListItem>
            <asp:ListItem Text="6" Value="6"></asp:ListItem>
            <asp:ListItem Text="7" Value="7"></asp:ListItem>
            <asp:ListItem Text="8" Value="8"></asp:ListItem>
            <asp:ListItem Text="9" Value="9"></asp:ListItem>
            <asp:ListItem Text="10" Value="10"></asp:ListItem>
            <asp:ListItem Text="11" Value="11"></asp:ListItem>
            <asp:ListItem Text="12" Value="12"></asp:ListItem>
            <asp:ListItem Text="13" Value="13"></asp:ListItem>
            <asp:ListItem Text="14" Value="14"></asp:ListItem>
            <asp:ListItem Text="15" Value="15"></asp:ListItem>
            <asp:ListItem Text="16" Value="16"></asp:ListItem>
            <asp:ListItem Text="17" Value="17"></asp:ListItem>
            <asp:ListItem Text="18" Value="18"></asp:ListItem>
            <asp:ListItem Text="19" Value="19"></asp:ListItem>
        </asp:CheckBoxList>
        <asp:Label ID="checkBoxSelectedIndex" runat="server" ></asp:Label>
        <script type="text/javascript">
            function setChangedCheckbox(element) {
                var hiddenField = document.getElementById('<%=changedCheckBoxID.ClientID %>');
                if (hiddenField) {
                    hiddenField.value = element.id;
                }
            }
        </script>
        <asp:HiddenField ID="changedCheckBoxID" runat="server" />

        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 SelectedIndexCh anged event I retrieve the index from the id of the checkbox that was changed:

        Code:
        Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
          For Each item As ListItem In MyCheckboxList.Items
            item.Attributes.Add("onclick", "setChangedCheckbox(this)")
          Next
        End Sub
        
        Private Sub MyCheckboxList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyCheckboxList.SelectedIndexChanged
          'checkBoxSelectedIndex.Text = CheckBoxList1.SelectedIndex.ToString
        
          Dim regex As New Regex("[0-9]*$")
          checkBoxSelectedIndex.Text = regex.Match(changedCheckBoxID.Value).Value
        End Sub


        -Frinny
        Last edited by Frinavale; Aug 4 '10, 02:58 PM.

        Comment

        • Rocky4Q
          New Member
          • Aug 2010
          • 4

          #5
          Acutally the SelectedIndex property in the SelectedIndexCh anged 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.

          Comment

          • malique84
            New Member
            • Jun 2010
            • 2

            #6
            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.

            Originally posted by Rocky4Q
            Acutally the SelectedIndex property in the SelectedIndexCh anged 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.

            Comment

            • marcio Coelho
              New Member
              • Aug 2016
              • 1

              #7
              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!!!

              Comment

              Working...