无法从代码后面定位控件。

时间:2021-02-08 15:52:46

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

我有一个问题,就是要从后面的代码中手动创建控件/控件id。在做了研究之后,我发现如果我创建表及其所有组件,包括Page_Init()方法中的任何控件,那么在返回postback后,文本框控件的文本值应该可用到页面。我尝试使用FindControl()方法定位文本框控件。当调试时,它只到达我试图使用FindControl()定位控件的行,然后抛出一个异常“对象引用,而不是对象的实例”,bellow是Page_Init()方法。

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.

下面是按钮单击事件,以显示控件id及其文本。在过去的几天里,我一直坚持这样做。如有任何帮助,我们将不胜感激。

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 个解决方案

#1


0  

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.

也许我遗漏了一些东西,但是为什么不把所有的控件放在类的全局范围内(而不是在Page_Init中创建实例),这样您就可以在类的任何部分访问它们。当然,我假设Page_Init和getBoxinput_Click在同一个类中。

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.

这样,您就可以在任何函数中访问tb。

#2


0  

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>

#1


0  

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.

也许我遗漏了一些东西,但是为什么不把所有的控件放在类的全局范围内(而不是在Page_Init中创建实例),这样您就可以在类的任何部分访问它们。当然,我假设Page_Init和getBoxinput_Click在同一个类中。

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.

这样,您就可以在任何函数中访问tb。

#2


0  

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>