0

In C#, I want to assign all 10 Checklistboxes on my page with a value (e.g. 1). What is the syntax so that I can use a variable? I.e. checklistbox(i).selectedIndex = 1;

for (int i = 1; i < 11; i++)
{
  checklistbox1.selectedIndex = 1;
  checklistbox2.selectedIndex = 1;
  checklistbox3.selectedIndex = 1;
  ...
  checklistbox10.selectedIndex = 1;
}
1
  • 3
    You should name your controls. Commented Sep 19, 2011 at 20:32

3 Answers 3

2

I guess you should use "FindControl" method inorder to do that as shown below.

 for (int i = 1; i <= 10; i++)
 {
      (Page.FindControl("checklistbox" + i) as CheckBox).SelectedIndex = 1;
 }

"checklistbox" is assumed as "ID" that is prefixed for all the checkboxes.

Hope this helps!!

Sign up to request clarification or add additional context in comments.

3 Comments

I tried this, but I unfortunately it doens't recognize the SelecteIndex......'System.Web.UI.WebControls.CheckBox' does not contain a definition for 'SelectedIndex' and no extension method 'SelectedIndex' accepting a first argument of type 'System.Web.UI.WebControls.CheckBox' could be found
If you are using a checkbox, you can only set the "Checked" property of it. If you are using a checkboxlist, you can set the "SelectedIndex" but only of the items in the list and not for all of them. Please review your requirement.
I am using many CheckListBoxes that each have 3 "checks". The user can only select 1 value at a time because I have javascript (mutual exclusion function) running on their browswers. Appreciate your help!
1

You can loop over all the controls on the page and pick out the ones you need as described in this blog post by Kris Steele:

foreach (Control masterControl in Page.Controls)
{
    if (masterControl is MasterPage)
    {
        foreach (Control formControl in masterControl.Controls)
        {
            if (formControl is System.Web.UI.HtmlControls.HtmlForm)
            {
                foreach (Control contentControl in formControl.Controls)
                {
                    if (contentControl is ContentPlaceHolder)
                    {
                        foreach (Control childControl in contentControl.Controls)
                        {
                            if(childControl is CheckBoxList)
                            {
                                ((CheckBoxList)childControl).SelectedIndex = 1;
                            }
                        }
                    }
                }
            }
        }
    }
}

This may not be a good idea if you have a lot of controls on the page.

1 Comment

This could get gucky because I do have quite a few controls on my page. The page is essentially a large checklist.
0

You should create a List<CheckBoxList>:

var cbs = new List<CheckBoxList> { thingy, otherThingy, ... };

foreach (var cb in cbs)
     cb.SelectedIndex = 0;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.