0
votes

I'm having a problem to get controls/ controls id that I have created manually from code behind. after doing research I found if I create the table and its all component including any controls in Page_Init() method, when rendering after postback the text value of textbox control should be available to the page. i tried to locate the textbox control using FindControl() method. when debugging it only reach to the line where I tried to locate the control using FindControl() and then throw an exception "Object reference not set to an instance of an object" bellow is the Page_Init() method

protected void Page_Init(object sender, EventArgs e)
    {
        Table tb = new Table();
        tb.ID = "Table1";
        TableRow row1 = new TableRow();
        TableCell cell1 = new TableCell();
        TableCell cell2 = new TableCell();
        TableCell cell3 = new TableCell();
        TextBox txtbx = new TextBox();
        Button btn = new Button();

        cell1.Text = "Name: ";

        txtbx.ID = "table1_text_input";
        txtbx.ValidationGroup = "rosy";

        cell2.Controls.Add(txtbx);
        btn.Text = "Get the input";
        btn.ValidationGroup = "rosy";
        btn.Click += getBoxinput_Click;
        cell3.Controls.Add(btn);

        // adding cells to row1
        row1.Cells.Add(cell1);
        row1.Cells.Add(cell2);
        row1.Cells.Add(cell3);

        // adding row to table1
        tb.Rows.Add(row1);
        Panel1.Controls.Add(tb);

    }

below is the button click event that supposes to display the control id and its text. I'm stuck with this for the last couple of days. any help will be appreciated.

protected void getBoxinput_Click(object sender, EventArgs e)
    {
        try
        {
            if (IsPostBack)
            {
                Table t = (Table)Page.FindControl("Panel1").FindControl("Table1");
                TextBox tbox;

                foreach (TableRow tr in t.Rows)
                {
                    foreach (TableCell tc in tr.Cells)
                    {
                        foreach (Control cnt in tc.Controls)
                        {
                            if (cnt.GetType() == typeof(TextBox))
                            {
                                tbox = (TextBox)cnt;
                                display.Text += "control id: " + tbox.ID + " control input: " + tbox.Text + "<br/>";
                            }
                        }
                    }
                }
            }


        }
        catch (NullReferenceException ex)
        {

            display.Text += ex.Message;
        }

    }
2

2 Answers

0
votes

Maybe I am missing something but why don't you just put all the controls in the global scope of the class (instead of just creating the instances inside Page_Init) so that you can access them in any part of your class. Of course I am assuming that Page_Init and getBoxinput_Click are in the same class.

EDIT:

Here is the example on how to put the variables in the global scope:

Table tb; //Declare variables outside any function.

protected void getBoxinput_Click(object sender, EventArgs e)
{
    tb = new Table(); //Initialize them inside a function.
}

This way you will be able to access tb inside any function.

0
votes

Your code is working perfectly. Here is what I used for the form markup:

<form id="form1" runat="server">
    <asp:Panel ID="Panel1" runat="server"></asp:Panel>
    <asp:Label ID="display" runat="server"></asp:Label>
</form>