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
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Mahyar Kakaee
  • 82
  • 1
  • 3
  • 15

1 Answers1

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

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Damith
  • 62,401
  • 13
  • 102
  • 153
  • 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. – Mahyar Kakaee Nov 12 '11 at 14:13