【问题标题】:Programmatically created CheckBoxList not firing when "unchecked"“未选中”时以编程方式创建的 CheckBoxList 未触发
【发布时间】:2011-04-14 05:18:38
【问题描述】:

我正在使用 ASP.NET 和 C#。我正在以编程方式创建一个复选框列表。当我检查一个项目时, SelectedIndexChanged 事件正在触发。但是,当我取消选中该项目时,不会触发该事件。我在每次回发时绑定项目,并且自动回发设置为 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));
        }
    }

【问题讨论】:

    标签: asp.net checkboxlist


    【解决方案1】:

    我不明白如何将控件添加到容器中? 我刚刚检查过,并且在检查和取消检查时都触发了该事件。

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

    【讨论】:

    • 哇!它确实会在选中和取消选中时触发!!!我实际上正在构建复选框列表并将其绑定到 AJAX Accordion 控件。我已经用实际代码更新了我的问题。你能看到发生了什么吗?
    • 我只能猜测该问题与 AccordionPanel 有关。不幸的是,我没有这方面的经验。尝试构建一个尽可能简单的测试用例(只有一个 AccordionPanel、一组固定的项目等),这样您就可以找到问题的根源。祝你好运!
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-07
      • 1970-01-01
      • 1970-01-01
      • 2011-04-30
      相关资源
      最近更新 更多