列表不添加复选框中的文本

时间:2014-08-13 16:53:45

标签: c# asp.net checkbox

我有一个包含4行状态复选框的表(NJ,CA,FL等)

该表有一个ID和一个runat ="服务器"标签,每个复选框都有一个' ID' '文本'和' runat ="服务器"'标签

我尝试制作一个如下所示的列表,以便在查询中使用,但它没有得到添加

List<string> query = new List<string>();
foreach (Control c in tableStates.Controls)
{
    if (c is CheckBox)
    {
        CheckBox chk = (CheckBox)c;
        if (chk.Checked)
        {
            query.Add(chk.Text);
        }
    }
}
string line = string.Join(" or ", query.ToArray());
tbTest.Text = line;

我之前使用了一个表,但是我使用了form1.Controls并且它在foreach中工作,但我不想使用form1,以防除了页面上的状态之外还有其他复选框。

使用form1.Controls是我唯一的选择或这里发生了什么

无论如何,这是一行表

<table runat="server" id="tableStates">
    <tr>
        <td><asp:CheckBox ID="Checkbox0" Text="AL" runat="server" /></td>
        <td><asp:CheckBox ID="Checkbox1" Text="AK" runat="server" /></td>
        <td><asp:CheckBox ID="Checkbox2" Text="AZ" runat="server" /></td>
        <td><asp:CheckBox ID="Checkbox3" Text="AR" runat="server" /></td>
        <td><asp:CheckBox ID="Checkbox4" Text="CA" runat="server" /></td>
        <td><asp:CheckBox ID="Checkbox5" Text="CO" runat="server" /></td>
        <td><asp:CheckBox ID="Checkbox6" Text="CT" runat="server" /></td>
        <td><asp:CheckBox ID="Checkbox7" Text="DE" runat="server" /></td>
        <td><asp:CheckBox ID="Checkbox8" Text="DC" runat="server" /></td>
        <td><asp:CheckBox ID="Checkbox9" Text="FL" runat="server" /></td>
        <td><asp:CheckBox ID="Checkbox10" Text="GA" runat="server" /></td>
        <td><asp:CheckBox ID="Checkbox11" Text="HI" runat="server" /></td>
        <td><asp:CheckBox ID="Checkbox12" Text="ID" runat="server" /></td>
    </tr>
</table>

2 个答案:

答案 0 :(得分:3)

您需要遍历表中的所有TableRows和TableCell以获取CheckBox控件。

List<string> query = new List<string>();
foreach (HtmlTableRow tr in tableStates.Controls)
{
    foreach (HtmlTableCell tc in tr.Cells)
    {
        foreach (Control c in tc.Controls)
        {
             if (c is CheckBox)
             {
                  CheckBox chk = (CheckBox)c;
                  if (chk.Checked)
                  {
                       query.Add(chk.Text);
                  }
              }
        }
    }

}
string line = string.Join(" or ", query.ToArray());
tbTest.Text = line;

答案 1 :(得分:3)

或尝试使用这样的扩展类:

public static class ControlExtensions
    {
        public static IEnumerable<T> GetAllControlsOfType<T>(this Control parent) where T : Control
        {
            var result = new List<T>();
            foreach (Control control in parent.Controls)
            {
                if (control is T)
                {
                    result.Add((T)control);
                }
                if (control.HasControls())
                {
                    result.AddRange(control.GetAllControlsOfType<T>());
                }
            }
            return result;
        }
    }

在这种情况下,您可以像以下一样使用它:

var checkboxes = tableStates.GetAllControlsOfType<CheckBox>();
string line = string.Join(" or ", ctrls.ToArray<CheckBox>()
                .Where<CheckBox>(x => x.Checked == true)
                .Select(x => x.Text));