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

  • Home
  • SEARCH
  • 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 7168501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:47:36+00:00 2026-05-28T14:47:36+00:00

Ok so I have a checkbox list with n checkboxes. Everytime I check a

  • 0

Ok so I have a checkbox list with n checkboxes. Everytime I check a box I want that number to get added to a list (Chosen). When the box is unchecked I want to remove it from the list. Then when I hit a button I want to display the list in a textbox. Here is my code:

public partial class _Default : System.Web.UI.Page
{
    List<int> Chosen = new List<int>();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Chosen"] == null)
        {
            Session["Chosen"] = new List<int>();
        } 

        Chosen = (List<int>)Session["Chosen"];

        for (int i = 0; i < Numbers.Items.Count; i++)
        {
            if (Numbers.Items[i].Selected) { Chosen.Add(i + 1); }
            else { Chosen.Remove(i + 1); }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (var i in Chosen)
        {
            TextBox1.Text = Chosen[i].ToString();
        }
    }
}

Needless to say it does not work the way I want it to. When I select 1 and hit the button it writes 1 into the box, but if I unselect it and hit the button it leaves 1 there. Also if I select any number other than 1 I get an error on this line:

TextBox1.Text = Chosen[i].ToString();

Saying index is out of range. Yet when I debug the numbers seem to be allocating to the list properly.

  • 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-05-28T14:47:37+00:00Added an answer on May 28, 2026 at 2:47 pm

    Is this what you need?

    <asp:TextBox ID="textBox1" runat="server">
    </asp:TextBox>
    <asp:CheckBoxList ID="Numbers" runat="server" 
                       OnSelectedIndexChanged="UpdateCheckBoxex" AutoPostBack="true">
        <asp:ListItem Text="One" Value="One" />
        <asp:ListItem Text="Two" Value="Two" />
        <asp:ListItem Text="Three" Value="Three" />
        <asp:ListItem Text="Four" Value="Four" />
    </asp:CheckBoxList>
    
    
    protected void UpdateCheckBoxex(object sender, EventArgs e)
    {
        string s = string.Empty;
        new List<ListItem>(Numbers.Items.Cast<ListItem>().Where(i => i.Selected)).ForEach(x => s += string.Format("{0}, ", x.Text));
        textBox1.Text = s;        
    }
    

    In this case you don’t need Chosen, but if you still need to populate it, you dont need foreach loop

    List<int> Chosen = new List<int>();
    Chosen.AddRange(Numbers.Items.Cast<ListItem>().Where(i => i.Selected).Select(x => Numbers.Items.IndexOf(x) + 1));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of checkboxes that I want to insert into a database,
I have a list box with Checkboxes in it. I want to prevent the
I have a list of checkboxes, that I'd like, when clicked to be added
I have a list of checkboxes and i have a select_all checkbox. Please check
I have a checkbox list control on my asp.net web form that I am
We have an ASP.Net page that uses a checkbox to toggle between a list
I have a list of checkboxes and with every checkbox, there is an input
I have a that has a list of checkboxes and user can click any
I have a list of checkboxes, looking like this: <table> <tr><td><input type=checkbox /> <label>Bla</label></td></tr>
I have a list of checkboxes that look like below, the numbers, 416, 419,

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.