Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8834249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:58:13+00:00 2026-06-14T08:58:13+00:00

I have a simple Repeater that contains a checkbox and a Full name per

  • 0

I have a simple Repeater that contains a checkbox and a Full name per each row.
In addition, I have an “Add Name” button that adds a new full name to the database.

Supposingly user checks a few checkboxes and decides to add another name, I would like to be able to add a new name to the repeater without losing the information in the checkboxes that have been already checked.

I understand some javascript code might do the trick the question is how to approach it?
What do I do?

thanks in advance

p.s.
I’ll be glad to hear any advice, not olny regarding js.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:58:14+00:00Added an answer on June 14, 2026 at 8:58 am

    Here is quick solution, not very pretty but get the job done. Hope it will give you some new ideas

    Default.aspx.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace RepeaterCheckbox
    {
        public partial class _Default : System.Web.UI.Page
        {
            [Serializable]
            public class Person
            {
                public int Id { get; set; }
                public string Name { get; set; }
            }
    
            List<Person> personsFromDatabase
            {
                get { return (List<Person>)ViewState["persons"]; }
                set { ViewState["persons"] = value; }
            }
    
            //here we will store our person selection state
            Dictionary<int,bool> personSelectionState
            {
                get { return (Dictionary<int, bool>)ViewState["data"]; }
                set { ViewState["data"] = value; }
            }
    
            protected override void OnLoad(EventArgs e)
            {
                if (!IsPostBack)
                {
                    #region Test data
                    personsFromDatabase = new List<Person>{
                        new Person { Id = 1, Name = "Paul", },
                        new Person { Id = 2, Name = "Tom", },
                    };
                    #endregion
    
                    Bind(false);
                }
                base.OnLoad(e);
            }
    
            void Bind(bool isPostback)
            {
                if (!isPostback)
                {
                    //initialize person selection mapping
                    personSelectionState = new Dictionary<int, bool>();
                    foreach (Person person in personsFromDatabase)
                    personSelectionState.Add(person.Id, false);
                }
    
                //map persons to anonymous type that will help us define necessary values
                rpPersons.DataSource = personsFromDatabase.Select(x => new
                {
                    Id = x.Id,
                    Name = x.Name,
                    //get stored selection state for person
                    Selected = personSelectionState[x.Id],
                });
                rpPersons.DataBind();
            }
    
            protected void btnAddPerson_Click(object sender, EventArgs e)
            {
                //update selection states
                UpdateSelectionStatuses();
    
                if (!String.IsNullOrEmpty(txbName.Text))
                {
                    //add new person
                    personsFromDatabase.Add(new Person
                        {
                            Id = personsFromDatabase.Count +1,
                            Name = txbName.Text,
                        });
    
                    //add status mapping for new person so there is no error on postback binding
                    personSelectionState.Add(personsFromDatabase.Count, false);
    
                    //Refresh data on page, to see new person
                    Bind(true);
                }
            }
    
            void UpdateSelectionStatuses()
            {
                //loop through all items
                for (int i = 0; i < rpPersons.Items.Count; ++i)
                {
                    RepeaterItem repeaterItem = rpPersons.Items[i];
    
                    //find checkbox for item
                    var checkbox = (CheckBox)repeaterItem.FindControl("chbSelected");
                    if (checkbox != null)
                    {
                        //get our custom attribute
                        int id = int.Parse(checkbox.Attributes["personId"]);
    
                        //update stored checkbox status
                        personSelectionState[id] = checkbox.Checked;
                    }
                }
            }
    
            protected void rpPersons_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    var item = e.Item.DataItem;
    
                    var checkbox = (CheckBox)e.Item.FindControl("chbSelected");
                    if (item != null && checkbox != null)
                    {
                        //get person id from our helper anonymous type
                        System.Reflection.PropertyInfo[] anonymousTypeProperties = item.GetType().GetProperties();
                        int id = (int)anonymousTypeProperties.Where(x => x.Name == "Id").FirstOrDefault().GetValue(item, null);
    
                        //set custom attribute on checkbox to map checkbox with person
                        checkbox.Attributes["personId"] = id.ToString();
                    }
                }
            }
        }
    }
    

    Default.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RepeaterCheckbox._Default" %>
    

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Repeater runat="server" ID="rpPersons" OnItemDataBound="rpPersons_ItemDataBound" >
                    <ItemTemplate>
                        <p>
                            <asp:CheckBox ID="chbSelected" runat="server" AutoPostBack="false" Checked='<%# DataBinder.Eval(Container.DataItem, "Selected") %>' />
                            <asp:Label ID="lblName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>' />
                        </p>
                    </ItemTemplate>
                </asp:Repeater>
                <div>
                    <asp:TextBox ID="txbName" runat="server" />
                    <asp:Button ID="btnAddPerson" runat="server" Text="Add person" OnClick="btnAddPerson_Click" />
                </div>
            </div>
        </form>
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple repeater that gets the 'groups' of 'widgets' The home page
I have a repeater that holds a Radio button list, a textbox and a
I have simple php validation form that is halfway working. If you leave the
I have simple win service, that executes few tasks periodically. How should I pass
I have simple issue setting a two-way databinding of a checkbox in Silverlight 3.0.
I have simple form. <form target=_blank action=somescript.php method=Post id=simpleForm> <input type=hidden name=url value=http://...> <input
I have a view which contains (among other columns) a header name, and an
I typically render comments in a simple repeater. I have a social app which
I have a simple foreach loop that goes through the productID's I have stored
I have a simple C# application that uses UDP multicast in a single-receiver, single-sender

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.