Search code examples
c#asp.netdynamic-data

How to find a control recursively in ASP.NET


There are lots of examples on this, and I'm pretty sound with the concept of using recursion to find the control. Once the control has been found on postback, it can be interacted with, etc.

I have an empty asp:table in my HTML markup:

<asp:Table ID="editDataTable" runat="server">
</asp:Table>

And on Page_Load the table is populated with many rows and many columns (I am quite proud that I figured that out). Inside some of the table cells there is a <asp:TextBox />.

You've guessed it, I need to get the value of these text boxes!

(I've got the code for recursion and checked it, and it seems good.)

My table has two columns. The left one contains titles like "Company Name, Telephone", etc. and the right column contains text boxes with its respective title's value. So a user can edit the text box (if the telephone number has change for example) and submit the changes.

Obviously the rows are dynamically added depending on the user.

The problem I'm having is: You need to ADD the control to the page when populating the table. Something along the lines of:

myTable.Control.Add(new TextBox());

In my case, my Table is called editDataTable. So in my code where I add the Rows, I've added the control too, as follows.

for (int i = 0; i < rowCount; i++)
{
    editDataTable.Rows.Add(tblRow[j]); // This is where I add the ROW to my sexy table
    editDataTable.Controls.Add(new TextBox()); // This is where I add the control
}

Those awake amongst you will know that you can't add a text box control to a table!

So finally, my questions are:

  • How do I add the control for The Text Boxes in my table?
  • Where do I add them?
  • Is there any extra advice to help me fulfill my quest of retrieving the text values of my dynamically added text boxes?

Here's my recursive code just in case you were curious:

private void getControls(Control parent)
{
    foreach (Control c in parent.Controls)
    {
        if (c is TextBox && c.ID == null)
        {
            //Stuff
        }

        if (c.Controls.Count > 0)
        {
            getControls(c);
        }
    }
}

Solution

  • Here is an example of building a dynamic table in ASP.NET during PageLoad, and reading in the values through a postback. Since the table is dynamic, it will NOT be rebuilt when the page is postback to the server. If you want to rebuild the table, you'll need to render it again and use the values you pull in from Request.Form to re-populate it.

    HTML Markup

        <asp:Table ID="editDataTable" runat="server">
        </asp:Table>
        <asp:Button runat="server" Text="Submit" OnClick="Submit_Click" />
    

    Code Markup

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string[] dynamicTable = { "First Name", "Last Name", "Address", "Phone" };
    
                foreach (string s in dynamicTable)
                {
                    TableRow row = new TableRow();
                    // Add First Cell with Text to Row
                    row.Cells.Add(new TableCell() { Text = s });
    
                    // Create Second Cell
                    TableCell textBoxCell = new TableCell();
    
                    // Add Textbox Control to Second Cell
                    textBoxCell.Controls.Add(new TextBox() { ID = "Dynamic_" + s.Replace(" ","_") });
    
                    // Add Second Cell to Row
                    row.Cells.Add(textBoxCell);
    
                    // Add New Row to Table
                    editDataTable.Rows.Add(row);
                }
            }
        }
    
        protected void Submit_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < Request.Form.Count; i++)
            {
                string key = Request.Form.GetKey(i);
                if (key.Contains("Dynamic_"))
                {
                    Response.Write("<p><strong>" + (key.Remove(0,8)).Replace("_"," ") + "</strong>&nbsp;&nbsp;::&nbsp;&nbsp;" + Request.Form[i] + "</p>");
                }
            }
        }