通过FindControl

时间:2017-01-11 20:56:25

标签: c# asp.net webforms

我有asp:panel按钮点击,我会动态添加一些复选框。

点击另一个按钮,我需要查看这些复选框并检查它们是否已被选中。

我无法找到控件。我尝试使用我提供的ID以及ClientID进行FindControl。

复选框放入面板的标记,以及两个按钮:

<asp:Panel runat="server" ScrollBars="Vertical" ID="pnlEmailCheckboxes" Height="150">
    <br/>
    <asp:CheckBox runat="server" Text="Other" ID="cbOtherEmail"/>
    <asp:TextBox ID="txtOtherEmail" runat="server" Style="width: 270px;" CssClass="textbox-default"></asp:TextBox>
    <br/>
</asp:Panel>
<asp:LinkButton ID="btnSendEmail" Text="<span>Send Email</span>" runat="server" CssClass="page-footer-button-highlight" OnClick="btnSendEmail_Click"></asp:LinkButton>
<asp:LinkButton ID="btnCloseEmail" Text="<span>Close</span>" runat="server" CssClass="page-footer-button" CausesValidation="false" OnClick="btnCloseEmail_OnClick"></asp:LinkButton>

生成文本框的事件:

protected void btnEmail_Click(object sender, EventArgs e)
{
    List<CheckBox> cbList = new List<CheckBox>();
    for (int i = 0; i < 10; i++)
    {
        CheckBox cb = new CheckBox();
        cb.Text = "text" + i;
        cb.ID = Guid.newGuid().ToString();
        cb.ClientIDMode = ClientIDMode.Static;
        pnlEmailCheckboxes.Controls.AddAt(0, cb);
        pnlEmailCheckboxes.Controls.AddAt(0, new LiteralControl("<br/>"));
        cbList.Add(cb);
    }

    Session["checkboxes"] = cbList;
    mpeEmail.Show();
}

尝试检索文本框的按钮(不起作用):

protected void btnSendEmail_Click(object sender, EventArgs e)
{
    //the email recipients
    List<string> emailRecipients = new List<string>();

    List<CheckBox> cbList = (List<CheckBox>)Session["checkboxes"];

    foreach (CheckBox cb in cbList)
    {
        CheckBox cbClient = (CheckBox) pnlEmailCheckboxes.FindControl(cb.ClientID); //I've also tried to find it by cb.ID
        //null reference here, the checkbox cbClient was not found
        if (cbClient.Checked) emailRecipients.Add(cb.Text.Trim());
    }

    //Ive also tried this, it does not contain the dynamically generated checkboxes
    //var cbControls = pnlEmailCheckboxes.Controls.OfType<CheckBox>();
}

编辑:

客户端html甚至会显示正确的ID,该ID与我搜索的ID匹配。

<input id="00e3a485-2083-4ef8-810b-6ed4fb1f62f9" type="checkbox" name="ctl00$Body$00e3a485-2083-4ef8-810b-6ed4fb1f62f9">

2 个答案:

答案 0 :(得分:1)

尝试为每个动态控件添加唯一ID并设置ClientIDMode = ClientIDMode.Static

 List<CheckBox> cbList = new List<CheckBox>();
 for (int i = 0; i < 10; i++)
 {
    CheckBox cb = new CheckBox();
    cb.ID = "DynamicCb" + i";
    cb.ClientIDMode = ClientIDMode.Static;
    cb.Text = "text" + i;
    pnlEmailCheckboxes.Controls.AddAt(0, cb);
    pnlEmailCheckboxes.Controls.AddAt(0, new LiteralControl("<br/>"));
    cbList.Add(cb);
 }

答案 1 :(得分:0)

我找到了解决问题的方法。如果我在OnInit方法中添加控件,我可以在稍后的按钮中看到复选框。

protected override void OnInit(EventArgs e)
{
    List<CheckBox> cbList = new List<CheckBox>();
    for (int i = 0; i < 10; i++)
    {
        CheckBox cb = new CheckBox();
        cb.Text = "text" + i;
        cb.ID = Guid.newGuid().ToString();
        pnlEmailCheckboxes.Controls.AddAt(0, cb);
        pnlEmailCheckboxes.Controls.AddAt(0, new LiteralControl("<br/>"));
        cbList.Add(cb);
    }

    Session["checkboxes"] = cbList;
}

现在出现另一个问题,即使在UI上检查它们,每个复选框也始终具有Checked == False。然而,这是另一个问题的原因。