How to get the latest selected value from a checkbox list?
Asked Answered
W

3

12

I am currently facing a problem. How to get the latest selected value from a asp.net checkbox list?

From looping through the Items of a checkbox list, I can get the highest selected index and its value, but it is not expected that the user will select the checkbox sequentially from lower to higher index. So, how to handle that?

Is there any event capturing system that will help me to identify the exact list item which generates the event?

Waldowaldon answered 7/9, 2010 at 0:43 Comment(2)
By the latest you mean the last checkbox selected in the list?Manet
Use client side scripting like jQuery or something to do this... - jQuery.comProgramme
M
19

If I understood it right, this is the code I'd use:

protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
    int lastSelectedIndex = 0;
    string lastSelectedValue = string.Empty;

    foreach (ListItem listitem in CheckBoxList1.Items)
    {
        if (listitem.Selected)
        {
            int thisIndex = CheckBoxList1.Items.IndexOf(listitem);

            if (lastSelectedIndex < thisIndex)
            {
                lastSelectedIndex = thisIndex;
                lastSelectedValue = listitem.Value;
            }
        }
    }
}

Is there any event capturing system that will help me to identify the exact list item which generates the event?

You use the event CheckBoxList1_SelectedIndexChanged of the CheckBoxList. When a CheckBox of the list is clicked this event is called and then you can check whatever condition you want.

Edit:

The following code allows you to get the last checkbox index that the user selected. With this data you can get the last selected value by the user.

protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
    string value = string.Empty;

    string result = Request.Form["__EVENTTARGET"];

    string[] checkedBox = result.Split('$'); ;

    int index = int.Parse(checkedBox[checkedBox.Length - 1]);

    if (CheckBoxList1.Items[index].Selected)
    {
        value = CheckBoxList1.Items[index].Value;
    }
    else
    {

    }
}
Manet answered 7/9, 2010 at 1:2 Comment(6)
if the user selects the third checkbox, and THEN the second checkbox, wouldnt this function state that the third checkbox was the last selector value?Orangeade
From what I understood he wants to know the last checkbox selected in the list. If that's not the case, this code should be modified.Manet
i think he wants the "latest", not the "last" (ordered). i could be wrong tho.Orangeade
Thanks to you guys, but I want the last time selected value. For example, first time 3rd checkbox is selected and then 2nd checkbox is checked. I want to identify 2nd checkbox. I meant the checkbox which is checked last time.Waldowaldon
@Masud - that's what i thought. Does my answer help?Orangeade
@Masud Rahman - with the new code in my answer you can get the last index that the user selected. With this data you can get the selected value of the last checkbox clicked by the user.Manet
B
3

Below is the code which gives you the Latest selected CheckBoxList Item.

string result = Request.Form["__EVENTTARGET"];
string [] checkedBox = result.Split('$'); ;
int index = int.Parse(checkedBox[checkedBox.Length - 1]);

if (cbYears.Items[index].Selected)
{
  //your logic 
}
else
{
  //your logic 
}

Hope this helps.

Burl answered 7/2, 2012 at 9:55 Comment(0)
O
0

Don't know about you, but as a user i wouldn't want the page to post back every time a checkbox item was checked.

This is the solution i would go with (jQuery):

Declare a server-side hidden field on your form:

<asp:HiddenField ID="HiddenField1" runat="server" EnableViewState="true" />

Then wire up client-side event handlers for the checkboxes to store checkbox clicked:

$('.someclassforyourcheckboxes').click(function() {
   $('#HiddenField1').val($(this).attr('id'));

This is a lightweight mechanism for storing the ID of the "latest" checkbox clicked. And you won't have to set autopostback=true for the checkboxes and do an unecessary postback.

You dont HAVE to use jQuery - you can use regular Javascript, but, why do more work? =)

Then when you actually do the postback (on a submit button click i assume), just check the value of the hidden field.

Unless of course you WANT to postback on every checkbox click, but i can't envision a scenario in which you'd want this (maybe you're using UpdatePanel).

EDIT

The HTML of a checkbox list looks like this:

<input type="checkbox" name="vehicle" value="Bike" /> I have a bike

So, you can access three things:

Vehicle = $(this).attr('name');

Bike = $(this).attr('value');

I have a bike = $(this).html();

If you're trying to access the databound value, try the second technique.

Give that a try.

Orangeade answered 7/9, 2010 at 1:16 Comment(4)
Actually I was working with Coolite dropdownlist few days ago. That control has a property called SelectedItems which is an array of selecteditem according to the selection order.I was looking for something like that in asp.net checkbox list.Waldowaldon
Another thing is, I am using multiple selection for checkboxlist and checkbox is databound from DB. so using javascript, I just can track the ID, but not sure I can track the value populated for that checkbox. this value is not the text shown in checkbox rather it some other ID associated with checkbox which I can get from my code-behind page. Can you please post some example code for this: can you please post some code to handle databound checkbox with javascript?Waldowaldon
@Masud what exactly do you need to track? The value of the checkbox item? If so, just use $(this).html() which will give you the text of the checkbox item (ie the value)Orangeade
Keep in mind, checking a checkbox is a client-side event. ASP.NET cannot track the LAST selected item (not cleanly anyway). JavaScript is your only option - of course you could post back EVERYTIME a checkbox item is checked, but that's poor UX in my opinion.Orangeade

© 2022 - 2024 — McMap. All rights reserved.