views:

177

answers:

3

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 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?

Any help will be appreciated.

A: 

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
    {

    }
}
Leniel Macaferi
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?
RPM1984
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.
Leniel Macaferi
i think he wants the "latest", not the "last" (ordered). i could be wrong tho.
RPM1984
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.
Masud Rahman
I'll update my answer...
Leniel Macaferi
@Masud - that's what i thought. Does my answer help?
RPM1984
@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.
Leniel Macaferi
@Leniel: thx, your solution works. thanks all of you guys!
Masud Rahman
A: 

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.

RPM1984
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.
Masud Rahman
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?
Masud Rahman
@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)
RPM1984
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.
RPM1984
@Masud - answer updated to provide more help.
RPM1984
A: 

were you able to solve th issue? if yes could you provide me the same solution

Nikhil
got it .... thanks to Leniel Macaferi
Nikhil