this.Controls doesn't contain all controls

2019-06-09 22:04发布

问题:

One text box in particular has been skipped. That textbox with ID "q3TXT" is inside a panel which is hidden at the PageLoad method.

Yet it becomes visible before the function that collects controls.

Every other textbox is collected.

I've tried with IEnumberable collection of textboxes, which didn't work.

How can I make the textbox appear in the controls list?

Here is html:

<p id="question3Text" runat="server" class="question"></p>
<asp:RadioButtonList runat="server" ID="q3_RBList" AutoPostBack="true" OnSelectedIndexChanged="q3_RBList_SelectedIndexChanged">
</asp:RadioButtonList>
<asp:Panel runat="server" ID="q3Panel">
    <p>If no, when did data collection stop?</p>
    <asp:TextBox runat="server" ID="q3TXT"></asp:TextBox>
</asp:Panel><br /><br />

And a part of c# code:

foreach (var item in this.Controls.OfType<TextBox>())
{
    //some code
}

回答1:

that specific textBox is a part of your specific panel so you should look for it inside of panel.Controls instead of this.Controls