FindControl在c#中找不到复选框

时间:2014-12-22 03:23:44

标签: c# asp.net checkbox

我在转发器中创建了复选框:

<asp:Repeater ID="companyRepeater" runat="server">
    <ItemTemplate>
        <tr class ="DGItemStyle" id="myresultsRow1" runat="server" style='<%# SetBoxVisibilityReverse(Eval("compnName1").ToString()) %>'> 
            <td  style="padding:0.5em;"><%#Eval("compnAddress1") %></td> 
            .....
            <td><asp:CheckBox id="foo" runat="server" AutoPostBack="True" name="foo" /></td>
        </tr>

我想知道用户当前选择了哪个复选框,所以在代码隐藏中我做了:

CheckBox chkbox_All = FindControl("foo") as CheckBox;
if (chkbox_All != null)
{
   if (!chkbox_All.Checked)
   {
       Response.Write("No checked");
   }
   else
   {
       var IDs = chkbox_All.ClientID;
       Response.Write("ID here...");
   }
}

但我发现无论选择了多少个复选框,chkbox_All始终为null。 为什么会这样,如何在这种情况下找到选中的复选框?

1 个答案:

答案 0 :(得分:2)

您需要使用companyRepeater.Items块枚举foreach,如下所示

foreach (RepeaterItem ri in companyRepeater.Items)
{
    CheckBox chkbox_All = ri.FindControl("foo") as CheckBox;
    if (chkbox_All != null)
    {
       if (!chkbox_All.Checked)
       {
           Response.Write("No checked");
       }
       else
       {
           var IDs = chkbox_All.ClientID;
           Response.Write("ID here...");
       }
    }
}

修改

如果要显示已选中复选框的ID,请在转发器外的某处添加Literal

<asp:Literal ID="ltChecked" runat="server" />

如果选中该复选框,则将ID添加到ltChecked.Text

foreach (RepeaterItem ri in companyRepeater.Items)
{
    CheckBox chkbox_All = ri.FindControl("foo") as CheckBox;
    if (chkbox_All != null)
    {
       if (chkbox_All.Checked)
       {
           ltChecked.Text += chkbox_All.ClientID;
           ltChecked.Text += ", ";
       }
    }
}

编辑2

要在选中复选框时阻止页面重新加载,请从AutoPostBack="True"复选框中删除foo

<asp:Repeater ID="companyRepeater" runat="server">
    <ItemTemplate>
        <tr class = "DGItemStyle"  id="myresultsRow1" runat="server" style='<%# SetBoxVisibilityReverse(Eval("compnName1").ToString()) %>'> 
            <td  style="padding:0.5em;"><%#Eval("compnAddress1") %></td> 
            .....
            <td><asp:CheckBox id="foo" runat="server" name="foo" /></td>
        </tr>

编辑3

要从选中复选框的同一行获取compnName1值,请添加一个隐藏字段(hdCompName),其中包含转发器中的compnName1

<asp:Repeater ID="companyRepeater" runat="server">
    <ItemTemplate>
        <tr class = "DGItemStyle"  id="myresultsRow1" runat="server" style='<%# SetBoxVisibilityReverse(Eval("compnName1").ToString()) %>'> 
            <td  style="padding:0.5em;"><%#Eval("compnAddress1") %></td> 
            .....
            <td><asp:CheckBox id="foo" runat="server" name="foo" />
                <asp:HiddenField id="hdCompName" runat="server" Value='<%# Eval("compnName1") %>' />
            </td>
        </tr>

然后在后面的代码中获取hdCompName的值,如下所示

foreach (RepeaterItem ri in companyRepeater.Items)
{
    CheckBox chkbox_All = ri.FindControl("foo") as CheckBox;
    if (chkbox_All != null)
    {
        if (chkbox_All.Checked)
        {
            HiddenField hdCompName = ri.FindControl("hdCompName") as HiddenField;
            ltChecked.Text += hdCompName.Value;
            ltChecked.Text += ", ";
        }
    }
}