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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:26:54+00:00 2026-06-01T09:26:54+00:00

I am attempting to loop through an array of numbers, match them to the

  • 0

I am attempting to loop through an array of numbers, match them to the checkbox they they are associated with, and checkmark that box.

My checkbox is set up like:

<input type="checkbox" runat="server" id="someID" name="somename" value="1234" />

My code when the page loads currently looks like:

foreach (string interest in interests_Var){
   foreach (var c in Page.Controls)
   {
   }
}

interests_Var is my array containing different numbers. We’ll assume one of them is 1234. While looping through the page controls, I want to compare the value of the control to my number. If it equals my number, I want to then apply the attribute checked="checked". I’m assuming I have to find the ID of the control I am using, then use that ID to add a new attribute. Or is there a way I can add the attribute using the c variable?

I’m not dead set on this setup, so if you know a better way, I’m all ears. Thanks for any help and suggestions.

Thanks

Lots of good suggestions. I will try these in a bit. Got side-tracked on another project.

Solved

Ok, so here’s my end code. Thanks for the help James.

foreach (string interest in interests_Var)
{
       foreach (var chkCtrl in Panel1.Controls.OfType<CheckBox>())
       {
           if (chkCtrl.Attributes["value"].ToString() == interest.ToString())
          {
               chkCtrl.Checked = true;
          }
     }
}
  • 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-01T09:26:55+00:00Added an answer on June 1, 2026 at 9:26 am

    You would be best served putting the CheckBox controls into a Panel or a PlaceHolder, because then you won’t have to worry about recursive logic to traverse the control hierarchy.

    <asp:Panel ID="Panel1" runat="server">
        <asp:CheckBox ID="CheckBox1" runat="server" Text="Testing" value="1234" />
        ...
    </asp:Panel>
    

    By putting the controls into a container, you can just loop through the child controls of that element:

    foreach (string interest in interests_Var)
    {
        foreach (var chkCtrl in Panel1.Controls.OfType<CheckBox>())
        {
            chkCtrl.Checked = chkCtrl.Attributes["value"].Contains(interest.ToString());
        }
    }
    

    Assuming interests_Var is a List, here’s a shorter way to do it using LINQ:

    interests_Var.ForEach(str => plcInvoiceDetail.Controls.OfType<CheckBox>()
        .Select(chk => chk.Checked = chk.Attributes["value"] == str));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to loop through all of a maps annotations and check to
First of all, I'm uploading multiple images that goes through a for loop by
Using Excel Interop, I'm attempting to loop through all QueryTables in all Worksheets and
I am attempting to loop through rows in MySQL with the following code. Unfortunately,
I am trying to have an array with n elements permute through like so:
I'm returning data via JSON and attempting to loop through the JSON dataset to
Attempting to print out a list of values from 2 different variables that are
Attempting to make a NSObject called 'Person' that will hold the login details for
When attempting to call functions in math.h , I'm getting link errors like the
I am attempting to write some code that will expediently process video frames. I

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.