如何从一个复选框列表最新选择的价值呢? [英] How to get the latest selected value from a checkbox list?

查看:98
本文介绍了如何从一个复选框列表最新选择的价值呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前面临的一个问题。如何从一个asp.net复选框列表最新选择的价值呢?

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?

推荐答案

如果我的理解是正确的,这是code我会用:

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;
            }
        }
    }
}

是否有任何情况下捕捉系统,这将帮助我识别生成事件的确切列表项?

您使用的CheckBoxList的事件CheckBoxList1_SelectedIndexChanged。当点击列表中的复选框此事件被称为然后你可以检查任何你想要的状态。

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.

编辑:

以下code可让您获取用户选择的最后一个复选框指标。有了这些数据,你可以得到用户最后选择的值。

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
    {

    }
}

这篇关于如何从一个复选框列表最新选择的价值呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆