1

I would like to be able loop through all the html checkboxes in my form and if it is checked do something else do something else. I also would like to do this in the code behind not using any Javascript\jquery.

<form id="form1" runat="server">
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <div>
  <table width="850" border="0" cellpadding="0" cellspacing="10" class="Copy" >
    <tr>
      <td valign="top"><table width="200" border="0" cellspacing="0" cellpadding="3" bgcolor="#f0f4f8">
          <tr>
            <td width="21">&nbsp;</td>
            <td width="179"><strong>CheckBoxes</strong></td>
          </tr>
          <tr>
            <td><input runat="server" type="checkbox" name="checkbox1" id="checkbox1" /></td>
            <td>checkbox1</td>
          </tr>
          <tr>
            <td><input runat="server" type="checkbox" name="checkbox2" id="checkbox2" /></td>
            <td>checkbox2</td>
          </tr>
          <tr>
            <td><input runat="server" type="checkbox" name="checkbox3" id="checkbox3" /></td>
            <td>checkbox3</td>
          </tr>
        </table>
    </div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
    </form>

In the codebehind I have tried a couple of different ways but i am guessing since it is an HTML Input control of type Checkbox it is not working

 foreach (CheckBox chk in Page.Form.Controls.OfType<CheckBox>())
        {

            if (chk.Checked == true)
            {
                Label1.Text = "we have checkboxes";
            }
            else
            {
                Label1.Text = "Still no checkboxes";
            }

        }
Jawaid Akhtar
  • 41
  • 2
  • 9
  • Is there a reason you can't use an asp checkbox? Also this might help http://stackoverflow.com/questions/389149/how-to-access-html-form-input-from-asp-net-code-behind – tim Sep 08 '13 at 15:08
  • the form I am working with has over 50 checkboxes not that I cant use asp checkboxes it has already been built will all the logic to cater to html checkboxes it will be time consuming to change everything is all – Jawaid Akhtar Sep 08 '13 at 15:29
  • 1
    @Jawaid Akhtar-You can use `runat="server"` for the checkboxes.In the code you have to find controls of `HtmlInputCheckBox` type. Bad thing is you have to check if control is container and if it is, you have to find control inside it. Probably you have to use recursion to find controls inside control. – afzalulh Sep 08 '13 at 16:28
  • could you possible show me an example as I tried to find the child controls within my form with no success. So not sure if i am following what you are suggesting thanks – Jawaid Akhtar Sep 08 '13 at 17:38

2 Answers2

3

A couple of ways:

foreach (Control item in this.form1.Controls)
{
    //We just need HtmlInputCheckBox 
    System.Web.UI.HtmlControls.HtmlInputCheckBox _cbx = item as System.Web.UI.HtmlControls.HtmlInputCheckBox;
    if (_cbx != null)
    {
        if (_cbx.Checked)
        {
            //Do something: 
            Response.Write(_cbx.Name + " was checked.<br />");
        }
    }

}

or

//We just need HtmlInputCheckBox
IEnumerable<Control> _ctrls = from Control n in this.form1.Controls where n as System.Web.UI.HtmlControls.HtmlInputCheckBox != null select n;

if (_ctrls.Count() > 0)
{
    foreach (System.Web.UI.HtmlControls.HtmlInputCheckBox item in _ctrls)
    {
        if (item.Checked)
        {
            //Do something:
            Response.Write(item.Name + " was checked.<br/><br />");
        }
    }
}

Hope this helps....

EdSF
  • 11,753
  • 6
  • 42
  • 83
0

If you know the ID of the CheckBoxes, you can try using: FindControl Control.FindControl-Methode (String)

It wouldn't be a greate solution, biut try using this:

bool cFound = true;
while(cFounnd)
{
    var cCheckBox = this.FindControl(...);

    if(cCheckBox != null)
    {
        ....
    }
    else
    {
        cFound = false;
    }
}

EDIT: Maybe you try this: ControlFinder

Community
  • 1
  • 1
BendEg
  • 20,098
  • 17
  • 57
  • 131