“unchecked”时以编程方式创建的CheckBoxList未触发

时间:2011-04-14 05:18:39

标签: asp.net checkboxlist

我正在使用ASP.NET和C#。我正在编程创建一个复选框列表。检查项目时,SelectedIndexChanged事件正在触发。但是,当我取消选中该项时,事件不会被触发。我正在绑定每个回发上的项目,并且autopostback设置为true。我哪里错了?这是代码 -

page_load
{

    var cblUser = new CheckBoxList();
    cblUser.AutoPostBack = true;
    cblUser.SelectedIndexChanged += cblUser_SelectedIndexChanged;

    var list = DAL.GetUsers();
    foreach (var user in list)
    {
        cblUser.Items.Add(new ListItem(user.Name, user.Id));
    }
}

谢谢。

更新#1:实际代码 -

public partial class CategoriesAccordion : UserControl
    {
        public List<Community> AllCommunities
        {
            get
            {
                if (Session["AllCommunities"] == null)
                {
                    var db = new CommunityGuideDB();
                    Session["AllCommunities"] = db.Communities.OrderBy(x => x.Name).ToList();
                }
                return (List<Community>) Session["AllCommunities"];
            }
        }

        public List<Category> Categories
        {
            get
            {
                if (Session["Categories"] == null)
                {
                    var db = new CommunityGuideDB();
                    Session["Categories"] = db.Categories.OrderBy(x => x.Name).ToList();
                }
                return (List<Category>) Session["Categories"];
            }
        }

        public event EventHandler Categories_Selected = delegate { };

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) Session.Remove("Categories");
            LoadCategories();
        }

        private void LoadCategories()
        {
            foreach (var parent in Categories.Where(item => item.ParentId == null && item.ShowAsPivot == true).OrderBy(x => x.DisplayOrder))
            {
                var pane = new AccordionPane {ID = parent.Name};
                pane.HeaderContainer.Controls.Add(new LiteralControl(parent.Name));

                var cblValues = new CheckBoxList();
                cblValues.AutoPostBack = true;
                cblValues.SelectedIndexChanged += cblValues_SelectedIndexChanged;
                foreach (var child in Categories.Where(child => child.ParentId == parent.Id))
                {
                    var communityCount = child.CommunityCategory.Where(x => x.Categories_Id == child.Id).Count();
                    cblValues.Items.Add(new ListItem(string.Format("{0} ({1})", child.Name, communityCount), child.Id.ToString()));
                }

                pane.ContentContainer.Controls.Add(cblValues);
                acdFilters.Panes.Add(pane);
            }
        }

        protected void cblValues_SelectedIndexChanged(object sender, EventArgs e)
        {
            var cblValues = ((CheckBoxList) sender);
            var selectedCategories = (from ListItem item in cblValues.Items where item.Selected select Categories.Find(c => c.Id == new Guid(item.Value))).ToList();
            Categories_Selected(this, new CommandEventArgs("SelectedCategories", selectedCategories));
        }
    }

2 个答案:

答案 0 :(得分:2)

我不知道如何将控件添加到容器中? 我刚检查过,而且我在检查时发现了这个事件。取消选中。

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckBoxList cbList = new CheckBoxList();
        cbList.AutoPostBack = true;
        for (int i = 0; i < 10; i++)            
            cbList.Items.Add(i.ToString());
        cbList.SelectedIndexChanged += new EventHandler(cbList_SelectedIndexChanged);
        form1.Controls.Add(cbList);
    }

    void cbList_SelectedIndexChanged(object sender, EventArgs e)
    {
        //fires both on check & uncheck of an item
    }
}

答案 1 :(得分:0)

在您选择列表中的其他项目时会触发您绑定的SelectedIndexChanged事件,而不是在您检查项目时触发。 CheckBoxList没有用于更改其项目状态的事件。

尝试使用列表控件,如Repeater ...