Masud Rahman
Masud Rahman

Reputation: 1062

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

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?

Upvotes: 12

Views: 35951

Answers (3)

mehul9595
mehul9595

Reputation: 1955

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.

Upvotes: 3

RPM1984
RPM1984

Reputation: 73112

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.

Upvotes: 0

Leniel Maccaferri
Leniel Maccaferri

Reputation: 102408

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
    {

    }
}

Upvotes: 19

Related Questions