简体   繁体   English

通过FindControl找不到动态生成的控件

[英]Dynamically generated controls not being found via FindControl

I have a asp:panel that on a button click, I add some number of checkboxes to dynamically. 我有一个asp:panel ,单击按钮时,会动态添加一些复选框。

On another button click, I need to look at these checkboxes and check if they are checked. 在另一个按钮上单击,我需要查看这些复选框并检查是否已选中。

I can't find the controls. 我找不到控件。 I've tried doing FindControl with the ID that I've supplied it, as well as the ClientID. 我尝试使用提供的ID和ClientID进行FindControl。

The markup with the panel that the checkboxes get placed into, and the two buttons: 复选框所在的面板的标记,以及两个按钮:

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

The event that generates the textboxes: 生成文本框的事件:

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();
}

The button that tries to retrieve the textboxes (does not work): 尝试检索文本框的按钮(不起作用):

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>();
}

Edit: 编辑:

The client side html even shows the with the correct ID that matches the ID I'm searching for. 客户端html甚至显示,其ID与我要搜索的ID匹配。

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

Try adding a unique ID to each dynamic control and setting the ClientIDMode = ClientIDMode.Static : 尝试向每个动态控件添加唯一的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);
 }

I've found a solution for my problem. 我已经找到解决问题的方法。 If I add the controls in the OnInit method, I can see the checkboxes in the button click later on. 如果将控件添加到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;
}

Another problem is present now, each checkbox always has Checked==False even when they are Checked on the UI. 现在存在另一个问题,即使在UI上选中了每个复选框,每个复选框也始终具有Checked == False。 However, this is cause for another question. 但是,这是另一个问题的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM