如何从选中的 属性 更改的 CheckBoxList 中获取 ListItem?

How get the ListItem from CheckBoxList that it's Selected property has changed?

我有一个 CheckBoxList,当我 select 和 deselect item.I 尝试过此解决方案时,我想获取 ListItem

protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
    var selectedItem = CheckBoxList1.SelectedItem;
}

当我 select 一个项目时,它会 return 那 item.but 当我 deselect 一个项目时 return null(当任何项目是不是 selected) 或其他项目(当有另一个 selected 项目时)。

我现在应该如何获得我已经select删除的项目?

您可以创建一个会话变量来跟踪当前选定的项目:

protected void Page_Load(object sender, EventArgs e)
{
    CheckBoxList1.AutoPostBack = true;

    List<ListItem> selectedItems = Session["SelectedItems"] as List<ListItem>;
    if(selectedItems == null)
    {
        Session["SelectedItems"] = new List<ListItem>();
    }
}

然后每次选择的项目发生变化时,您可以将以前选择的项目与新选择的项目进行比较,找出选择或取消选择的项目:

protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
    // Gather currently selected items
    List<ListItem> newSelectedItems = new List<ListItem>();
    foreach (ListItem item in CheckBoxList1.Items)
    {
        if (item.Selected == true) 
        {
            newSelectedItems.Add(item);
        }
    }

    List<ListItem> oldSelectedItems = Session["SelectedItems"] as List<ListItem>;

    // Compare previous items count with current items count 
    // to find out whether item has been selected or deselected
    if (newSelectedItems.Count > oldSelectedItems.Count)
    {
        // Item has been selected
        ListItem selectedItem = newSelectedItems.Except(oldSelectedItems).First();
    }
    else
    {
        // Item has been deselected
        ListItem deselectedItem = oldSelectedItems.Except(newSelectedItems).First();
    }

    Session["SelectedItems"] = newSelectedItems;
}

我创建了 CustomControl Like this 并使用了 'Krzysztof Bracha' 解决方案。这样使用起来更简单

用户控件:

public class JooyaCheckBoxList : CheckBoxList
{
    public JooyaCheckBoxList()
    {
        base.SelectedIndexChanged += JooyaCheckBoxList_SelectedIndexChanged;
        base.PreRender += JooyaCheckBoxList_PreRender;
    }

    private void JooyaCheckBoxList_PreRender(object sender, EventArgs e)
    {
        SelectedItems = Items.Cast<ListItem>().Where(li => li.Selected).ToList();
    }
    private static List<ListItem> SelectedItems { get; set; }
    private void JooyaCheckBoxList_SelectedIndexChanged(object sender, EventArgs e)
    {
        List<ListItem> CurrentSelectedItems = Items.Cast<ListItem>().Where(li => li.Selected).ToList();
        if (CurrentSelectedItems.Count > SelectedItems.Count)
        {
            var li = CurrentSelectedItems.Except(SelectedItems).SingleOrDefault();
            OnSelectedPropertChaned(li);
        }
        else if (CurrentSelectedItems.Count < SelectedItems.Count)
        {
            var li = SelectedItems.Except(CurrentSelectedItems).SingleOrDefault();
            OnSelectedPropertChaned(li);
        }
        else
        {
            OnSelectedPropertChaned(null);
        }
    }

    public event EventHandler<ListItem> SelectedPropertyChaned;
    public void OnSelectedPropertChaned(ListItem changedItem)
    {
        SelectedPropertyChaned?.Invoke(this, changedItem);
    }
}

我是这样使用的:

    protected void cblKeywords_SelectedPropertyChaned(object sender, ListItem changedItem)
    {
            MessageDisplay= changedItem.Text + "  ||  " + changedItem.Value; 
    }