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 7604165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:50:26+00:00 2026-05-30T23:50:26+00:00

I am creating a CheckBoxList in a class file and am using an HTMLTextWriter

  • 0

I am creating a CheckBoxList in a class file and am using an HTMLTextWriter to render the control.

I’m using the following code to store the selected values in a string:

string YrStr = "";
for (int i = 0; i < YrChkBox.Items.Count; i++)
{
    if (YrChkBox.Items[i].Selected)
    {
        YrStr += YrChkBox.Items[i].Value + ";"; 
    }
}

I stepped through the code and it doesn’t seem to hit the inside of the if statement & the selected value attribute is false every time … Anyone have an idea of how I can address this?

I populate it using the following:

 YrChkBox.Items.Add(new ListItem("Item 1", "Item1"));
  • 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-30T23:50:28+00:00Added an answer on May 30, 2026 at 11:50 pm

    In your ASPX page you’ve got the list like this:

        <asp:CheckBoxList ID="YrChkBox" runat="server" 
            onselectedindexchanged="YrChkBox_SelectedIndexChanged"></asp:CheckBoxList>
        <asp:Button ID="button" runat="server" Text="Submit" />
    

    In your code behind aspx.cs page, you have this:

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Populate the CheckBoxList items only when it's not a postback.
                YrChkBox.Items.Add(new ListItem("Item 1", "Item1"));
                YrChkBox.Items.Add(new ListItem("Item 2", "Item2"));
            }
        }
    
        protected void YrChkBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Create the list to store.
            List<String> YrStrList = new List<string>();
            // Loop through each item.
            foreach (ListItem item in YrChkBox.Items)
            {
                if (item.Selected)
                {
                    // If the item is selected, add the value to the list.
                    YrStrList.Add(item.Value);
                }
                else
                {
                    // Item is not selected, do something else.
                }
            }
            // Join the string together using the ; delimiter.
            String YrStr = String.Join(";", YrStrList.ToArray());
    
            // Write to the page the value.
            Response.Write(String.Concat("Selected Items: ", YrStr));
        }
    

    Ensure you use the if (!IsPostBack) { } condition because if you load it every page refresh, it’s actually destroying the data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Repeater control that is creating a dynamic amount of CheckBoxList controls
Creating a zip file using Send To -> Compressed folder excludes .hg folder on
I'm using ASP.NET and C#. I'm programmtically creating a checkboxlist. When I check an
Creating servlet that implements contextInitializer interface in this code, then accessing file inside contextinitialized()
I am creating some checkboxes in a div using these line of code: for(j=0;j<allActivities.length;j++){
Creating a view with following code. - (void)loadView { paintView=[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
Creating a google map with store locations within 50 miles of user entered address.
Can we Make a checkboxList using jquery like we fill the options in select
Creating a table without tbody using javascript createElement/appendChild will not add tbody in Firebug
Creating sprite sheets aka texture atlases rather than using many hundres of individual images

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.