通过表单中的所有HTML复选框环 [英] Loop through all html checkboxes in form

查看:134
本文介绍了通过表单中的所有HTML复选框环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过我的表单中的所有HTML复选框能循环,如果它被选中做别的事情做别的事情。我也想这样做在code不使用任何JavaScript \\ jQuery的后面。

 <表ID =form1的=服务器>
        < ASP:标签ID =Label1的=服务器文本=标签>< / ASP:标签>
    < D​​IV>
  <表格的宽度=850BORDER =0的cellpadding =0CELLSPACING =10级=复制>
    &所述; TR>
      < TD VALIGN =顶><表格的宽度=200BORDER =0CELLSPACING =0的cellpadding =3的bgcolor =#f0f4f8>
          &所述; TR>
            < TD WIDTH =21>&安培; NBSP;< / TD>
            < TD WIDTH =179><强>的CheckBox< / STRONG>< / TD>
          < / TR>
          &所述; TR>
            < TD><输入=服务器类型=复选框NAME =checkbox1ID =checkbox1/>< / TD>
            < TD> checkbox1< / TD>
          < / TR>
          &所述; TR>
            < TD><输入=服务器类型=复选框NAME =checkbox2ID =checkbox2/>< / TD>
            < TD> checkbox2< / TD>
          < / TR>
          &所述; TR>
            < TD><输入=服务器类型=复选框NAME =checkbox3ID =checkbox3/>< / TD>
            < TD> checkbox3< / TD>
          < / TR>
        < /表>
    < / DIV>
        < ASP:按钮的ID =Button1的=服务器的OnClick =的button1_Click文本=按钮/>
    < /表及GT;

在codebehind我已经尝试了几种不同的方式,但我猜测,因为它是类型复选框它不工作的HTML输入控制

 的foreach(复选框CHK在Page.Form.Controls.OfType<&复选框GT;())
        {            如果(chk.Checked ==真)
            {
                Label1.Text =我们有复选框
            }
            其他
            {
                Label1.Text =仍然没有复选框
            }        }


解决方案

一对夫妇的方式:

 的foreach(在this.form1.Controls控制项)
{
    //我们只需要HtmlInputCheckBox
    System.Web.UI.HtmlControls.HtmlInputCheckBox _cbx =项目作为System.Web.UI.HtmlControls.HtmlInputCheckBox;
    如果(_cbx!= NULL)
    {
        如果(_cbx.Checked)
        {
            //做一点事:
            的Response.Write(_cbx.Name +被选中< BR />中);
        }
    }}

  //我们只需要HtmlInputCheckBox
IEnumerable的<控制> _ctrls =从控制n的this.form1.Controls,其中n为System.Web.UI.HtmlControls.HtmlInputCheckBox = NULL选择N!;如果(_ctrls.Count()大于0)
{
    的foreach(在_ctrls System.Web.UI.HtmlControls.HtmlInputCheckBox项)
    {
        如果(item.Checked)
        {
            //做一点事:
            的Response.Write(item.Name +被选中< BR />< BR />中);
        }
    }
}

希望这有助于....

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";
            }

        }

解决方案

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....

这篇关于通过表单中的所有HTML复选框环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆