ASP .NET Foreach TextBox Control In Html Table?

Posted on 2012-09-17
Medium Priority
Last Modified: 2012-09-18
Not sure why this is not working. I have several asp:TextBox in html table within asp:Form like so. Doesn't error though.

    <form id="form1" runat="server">
 <table id="table1" runat="server" >
            <td >
                    <asp:TextBox ID="textBox1" runat="server" Enabled="False" EnableTheming="True"
                        MaxLength="1" ReadOnly="True"></asp:TextBox>


                HtmlTable table = (HtmlTable)Page.FindControl("table1");
                List<string> txtList = new List<string>();
                foreach (Control ctl in table.Controls)
                    if (ctl.GetType().ToString() == "System.Web.UI.WebControls.TextBox")
                    //if(ctl is TextBox)
                           string s = ((TextBox)ctl).ID;

                foreach(string id in txtList)
                    System.Web.HttpContext.Current.Response.Write("TextBox.ID=" + id + "<br>");
Question by:pointeman
LVL 25

Accepted Solution

Luis Pérez earned 2000 total points
ID: 38408462
That's because when you're looping thru the table .Controls property, what you're really doing is looping thru the rows collection of the table. But the textboxes are not directly inside the table; they're inside cells that are inside rows that are inside the table. So you must do something like this:

HtmlTable table = (HtmlTable)Page.FindControl("table1");
foreach (HtmlTableRow row in table.Rows)
    foreach (HtmlTableCell cell in row.Cells)
        foreach (Control ctl in cell.Controls)
            if (ctl.GetType().ToString() == "System.Web.UI.WebControls.TextBox")

Open in new window

Hope that helps.

Author Closing Comment

ID: 38409115
Most excellent! Thx

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Screencast - Getting to Know the Pipeline
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question