以编程方式创建的 CheckBoxList 在“未选中”时不会触发

发布于 2024-11-01 01:17:35 字数 3159 浏览 0 评论 0原文

我正在使用 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));
        }
    }

I'm using ASP.NET and C#. I'm programmtically creating a checkboxlist. When I check an item, the SelectedIndexChanged event is firing. But, when I uncheck the item, the event is not fired. I'm binding the items on every postback and autopostback is set to true. Where am I going wrong? Here's the code -

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

Thank you.

Update #1: Actual code -

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

草莓味的萝莉 2024-11-08 01:17:35

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

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

I don't get how do you add the control to a container?
I've just checked and I've got the event fired both on checking & unchecking.

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
    }
}
不…忘初心 2024-11-08 01:17:35

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

尝试使用像 Repeater 这样的列表控件...

The SelectedIndexChanged event you are bounding is fired upon selecting different item on your list, not when you check an item. CheckBoxList does not have an event for changing the status of its items.

Try a to use list control like Repeater ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文