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

发布于 2024-11-18 13:21:28 字数 2397 浏览 3 评论 0原文

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 保存在列表中。我该如何继续。

提前致谢。即使您能给我一些想法,我也会很感激。再次感谢!

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

        }
}

So far so good. I'm handling the OnItemDataBound event to check those textboxes that show profiles with the property Profile.StatusID set to 1.

Now i want to capture all changes. Say if a user unchecks a checkbox or checkes a previously unchecked checkbox I'd like to save the ID's of those Profiles in a list. How do I proceed.

Thanks in advance. I'd appreciate even if you give me ideas to revolve around. Thanks again!

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

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

发布评论

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

评论(1

顾冷 2024-11-25 13:21:28

You need to handle the Checkbox changed event after this you can determine if it is your collection or not and add or remove it accordingly

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