复选框改变了状态?如何找出哪些复选框已更改其状态

时间:2011-07-04 17:51:29

标签: c# asp.net checkbox buttonclick

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Repeater_Checkbox
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            repeater.DataSource = PopulateCollection();
            repeater.DataBind();
        }

        public CollectionProfiles PopulateCollection()
        {
            var lista = new CollectionProfiles();

            var p1 = new Profile {ProfileDesc = "asdas", ProfileID = 1, ProfileStatus = 1};
            lista.Add(p1);

            var p2 = new Profile {ProfileDesc = "asdasd", ProfileID = 2, ProfileStatus = 0};
            lista.Add(p2);

            var p3 = new Profile {ProfileDesc = "nsadsdot", ProfileID = 3, ProfileStatus = 1};
            lista.Add(p3);

            var p4 = new Profile {ProfileDesc = "gluposti", ProfileID = 4, ProfileStatus = 1};
            lista.Add(p4);

            var p5 = new Profile {ProfileDesc = "asdaile", ProfileID = 5, ProfileStatus = 0};
            lista.Add(p5);

            var p6 = new Profile {ProfileDesc = "sdfsdf", ProfileID = 6, ProfileStatus = 1};
            lista.Add(p6);

            var p7 = new Profile {ProfileDesc = "dfsdf", ProfileID = 7, ProfileStatus = 1};
            lista.Add(p7);

            return lista;
        }

        protected void repeater_ItemDataBound(object source, RepeaterItemEventArgs e)
        {
            var taaLista = PopulateCollection();
            var someItem = (CheckBox)e.Item.FindControl("checkbox");
            var profileID = Convert.ToInt32(someItem.Attributes["data-id"]);

            foreach (var item in taaLista)
            {
                if ((item.ProfileID == profileID) && (item.ProfileStatus == 1))
                {
                    someItem.Checked = true;
                    return;
                }
                someItem.Checked = false;
            }
        }

        public class CollectionProfiles : Collection<Profile>
        {

        }
}

到目前为止一切顺利。我正在处理OnItemDataBound事件以检查那些显示属性Profile.StatusID设置为1的配置文件的文本框。

现在我要捕获所有更改。假设用户取消选中复选框或检查以前未选中的复选框,我想将这些配置文件的ID保存在列表中。我该怎么做。

提前致谢。即使你给我一些想法,我也会感激不尽。再次感谢!

1 个答案:

答案 0 :(得分:0)

您需要处理Checkbox changed event之后,您可以确定它是否是您的收藏品并相应地添加或删除