如何从CheckBoxList获取ListItem它的Selected属性是否已更改?

时间:2017-06-15 06:10:32

标签: c# asp.net webforms checkboxlist

我有一个CheckBoxList,当我选择并取消选择该项时,我想获取ListItem。我试过这个解决方案

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

当我选择一个项目时,它将返回该项目。但是当我取消选择一个项目时将返回null(当未选择任何项目时)或其他项目(当有另一个选定项目时)。

我应该如何获得我现在取消选择的项目?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

我创建了CustomControl,并使用了'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; 
    }