ASP .NET Foreach TextBox Control In Html Table?

Posted on 2012-09-17
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

    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

    Most excellent! Thx

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    732 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now