动态生成的复选框在回发时始终具有Checked == False

时间:2017-01-11 22:21:41

标签: c# asp.net checkbox webforms

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

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

这是我之前发布的一个跟进问题。第一个问题是我在回发时根本找不到动态生成的控件。

现在,我可以找到它们。问题是Checked属性始终为false,尽管检查了UI上的复选框。

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

<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
        //ALWAYS FALSE
        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">

1 个答案:

答案 0 :(得分:0)

需要在每次回发时重新创建动态控件(已经发生了,因为我在页面OnInit函数中创建了它们)。

然而,问题在于我使用新生成的Guid作为每个控件的ID。这导致每个回发的重新生成的控件不会映射到旧控件的postdata。

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(); //don't do this.
        cb.ID = "checkbox" + i; // <------ do this, have a consistent ID

        cb.ClientIDMode = ClientIDMode.Static;
        pnlEmailCheckboxes.Controls.AddAt(0, cb);
        pnlEmailCheckboxes.Controls.AddAt(0, new LiteralControl("<br/>"));
        cbList.Add(cb);
    }

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