如何从复选框列表中获取最新选定的值?

Mas*_*man 12 asp.net selecteditem checkboxlist

我目前正面临一个问题.如何从asp.net复选框列表中获取最新选定的值?

从循环到复选框列表的项目,我可以获得最高选择的索引及其值,但不希望用户从低到高的索引顺序选择复选框.那么,如何处理呢?

是否有任何事件捕获系统可以帮助我识别生成事件的确切列表项?

Len*_*rri 15

如果我理解正确,这是我使用的代码:

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;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

是否有任何事件捕获系统可以帮助我识别生成事件的确切列表项?

您使用CheckBoxList的事件CheckBoxList1_SelectedIndexChanged.单击列表的CheckBox时,将调用此事件,然后您可以检查所需的任何条件.

编辑:

以下代码允许您获取用户选择的最后一个复选框索引.使用此数据,您可以获得用户最后选择的值.

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
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我认为他想要"最新",而不是"最后"(订购).我可能是错的. (3认同)