0

I have a simple situation here. I have a web form with 'Accordion' which has some 'AccordionPanes' and in each AccordionPane there is some 'CheckBoxes'. About 30 checkboxes total.

Now I need to check the statuses of every checkboxes. and the question is how!? I was thinking about a 'for loop' and 'If Condition' like this:

for (i = 1; i <= 5; i++)
{
    if (CheckBox(i).Checked)
    {
        Label1.Text = "yeepee!";
    }
}

But looks like this is not a standard way to use 'if state' (and looks like I'm not a pro developer!). Now friends, Which way do you suggest?

for making the situation brighter, here is the HTML Code i'm using in my form:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:Accordion ID="Accordion1" 
                       runat="server"
                       HeaderCssClass="accordionHeader" 
                       HeaderSelectedCssClass="accordionHeaderSelected" 
                       ContentCssClass="accordionContent"  
                       SelectedIndex="0" 
                       FadeTransitions="false" 
                       FramesPerSecond="40" 
                       TransitionDuration="250" 
                       AutoSize="Fill" 
                       RequireOpenedPane="true" 
                       SuppressHeaderPostbacks="true">
            <Panes>
                <asp:AccordionPane ID="AccordionPane1" runat="server">
                    <Header>
                        <h1>title</h1>
                    </Header>
                    <Content>
                        <asp:CheckBox ID="CheckBox1" runat="server" Text="sometext" />
                        <asp:CheckBox ID="CheckBox2" runat="server" Text="sometext" />
                        <asp:CheckBox ID="CheckBox3" runat="server" Text="sometext" />
                        <asp:CheckBox ID="CheckBox4" runat="server" Text="sometext" />
                        <asp:CheckBox ID="CheckBox5" runat="server" Text="sometext" />
                        <asp:CheckBox ID="CheckBox6" runat="server" Text="sometext" />
                        <asp:CheckBox ID="CheckBox7" runat="server" Text="sometext" />
                    </Content>
                </asp:AccordionPane>
            </Panes>
            <Panes>
                <asp:AccordionPane ID="AccordionPane2" runat="server">
                    <Header>
                        <h1>title</h1>
                    </Header>
                    <Content>
                        <asp:CheckBox ID="CheckBox8" runat="server" Text="sometext" />
                        <asp:CheckBox ID="CheckBox9" runat="server" Text="sometext" />
                        <asp:CheckBox ID="CheckBox10" runat="server" Text="sometext" />
                    </Content>
                </asp:AccordionPane>
            </Panes>
            .
            <!--    some more Panes and Checkboxes!    -->
            .
        </asp:Accordion>
    </ContentTemplate>
</asp:UpdatePanel>

And of course ASP.Net is the platform and C#.net is the language. Thank you and I'm looking forward to your answers. regards.


Edited:

My only problem is exactly this! you can't use checkboxes in a loop like this: Checkbox(i).Checked:

   bool[] array = new bool[30];
   for (int i = 0; i < 30; i++)
   {
      array[i] = CheckBox(i).Checked ;
   }

I exactly want to know how can i use IDs of checkboxes with a variable, like:

i = 15;
CheckBox(i).Checked

instead of:

CheckBox15.Checked

1 Answer 1

2

Use array of bool

bool[] array = new bool[30];

for (int i = 0; i < 30 ; i++)  
{ 
 array [i] =CheckBox(i).Checked;
}

but problem will be get all textboxes in the page, you can try below code on page load

foreach (Control ctrl in Page.Controls) {
    if (ctrl is CheckBox) {
           array [i] =  ((CheckBox)ctrl).Checked;
    }
}

or you may try to do this using client side jQuery or JavaScript

Loop through checkboxes and count each one checked or unchecked

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

1 Comment

thank you @UnhandledException but that doesn't work, i can't tell why exactly but let't say it doesn't recognize ant checkboxes in the page.

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.