简体   繁体   中英

Creating table rows in codebehind

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

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

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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM