2

can someone please help me understand what needs to be done here since i'm new to C#.

I have one form with two buttons and a couple text boxes.

First, the user enters a number in one of the text boxes and then presses one of the buttons. there is also an empty in this form like this:

<asp:Table id="tblInputs" runat="server" Border="1" Width="100%">
</asp:Table>

So, when the user hits the first button the code behind will add as many rows to this table is the input in the text box, like this:

for (int i = 0; i < numOfInputFields; i++) 
{
    TableRow row = new TableRow ();
    TableCell cell_inputA = new TableCell ();
    TableCell cell_inputB = new TableCell ();

    TextBox txtBox_input = new TextBox ();

    txtBox_input.ID = "txtInFld" + (i + 1);
    txtBox_input.Text = txtBox_input.ID;
    cell_inputA.Text = "Input " + (i + 1);
    cell_inputB.Controls.Add (txtBox_input);

    row.Cells.Add (cell_inputA);
    row.Cells.Add (cell_inputB);

    tblInputs.Rows.Add (row);
}

Now so far this works fine and the columns and rows are created fine.

My question now is, since the IDs for the text box were created in codebehind above, how do i access them from other codebehind functions.

At top of the post i mentioned i have another button in the same form that just changes the value of text in one of the newly created text boxes, but it doesn't work.

The regular way of saying txtInFld1.Text = "something"; doesn't work any more because it doesn't see txtInFld1 even though it was already created above. I get "The name 'txtInFld1' does not exist in current context". I suspect it has something to do with resubmitting the same form but I'm not sure.

Can any one please explain what is happening here and how do i access the properties of the new text boxes created in codebehind?

Thanks

4
  • Can you post the code that doesn't work? You may have to use the FindControl() method to find the TextBox within the TableCell, but I can't be sure until I see what you've already tried. Commented Feb 20, 2013 at 17:21
  • Oh i didnt realize there is FindControl(). I did something similar with Delphi not to long ago and i used findControl() equivalent. I'll look into that when i get back home later on today. Thanks! Commented Feb 20, 2013 at 17:30
  • Give this article a read on Dynamic Control creation in ASP.NET it should demonstrate how to create, access and maintain state and answer your question! 4guysfromrolla.com/articles/092904-1.aspx Commented Feb 20, 2013 at 17:32
  • Very nice article on the topic, thanks a lot! Commented Feb 20, 2013 at 17:34

2 Answers 2

0

Dynamically created controls are a bit hard to understand at the beginning.

1) You need to save ids in ViewState to persist the data after post back.

2) Then recreate those textboxes back when the page is posted back. Otherwise, you won't be able to access it.

Here is similar answer (that question use UserControl instead of TextBox)-

https://stackoverflow.com/a/14449305/296861

Sign up to request clarification or add additional context in comments.

Comments

0

You can recursively burrow into the table's controls collection until you find the child control.

Pass in the parent control followed by the id of the control you're looking for.

Cheers, ~Codewolfe

    private System.Web.UI.Control FindControlRecursive(Control root, string id)
    {
        if (root.ID == id)
        {
            return root;
        }

        foreach (Control c in root.Controls)
        {
            Control t = FindControlRecursive(c, id);
            if (t != null)
            {
                return t;
            }
        }

        return null;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.