如何在ASP.NET中递归地找到控件

时间:2021-09-20 15:03:07

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:

我在HTML标记中有一个空的asp:table:

<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 />.

在Page_Load中,表中填充了许多行和许多列(我很自豪我知道了这一点)。在一些表单元格中,有一个

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.

在我的例子中,我的表被称为editDataTable。在我的代码中添加行,我也添加了控件,如下所示。

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

2 个解决方案

#1


1  

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.

下面是在ASP中构建动态表的一个例子。在PageLoad期间使用NET,并通过回发读取值。由于该表是动态的,所以在页面回发到服务器时不会重新构建该表。如果希望重新构建表,则需要再次呈现它,并使用从请求中提取的值。形式重新填充它。

HTML Markup

HTML标记

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

#2


1  

You need to add a TableCell to the TableRow.Cells collection, and add the TextBox to the TableCell.Controls collection:

您需要在TableRow中添加一个TableCell。单元格集合,并将文本框添加到TableCell中。控制集合:

TableCell cell = new TableCell();
cell.Controls  = new TextBox();
tblRow[j].Cells.Add(cell);
editDataTable.Rows.Add(tblRow[j]);

The most direct way to access the textboxes is to keep them all in a list:

访问文本框最直接的方式是把它们都放在一个列表中:

List<TextBox> textBoxes = new List<TextBox>();

and replace cell.Controls = new TextBox(); above with this:

和更换电池。控制= new文本框();上面这个:

TextBox tb = new TextBox();
textBoxes.Add(tb);
cell.Controls = tb;

And then you can iterate through the textBoxes afterward without having to search for them in a tree.

然后你可以遍历文本框,而不必在树中搜索它们。

#1


1  

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.

下面是在ASP中构建动态表的一个例子。在PageLoad期间使用NET,并通过回发读取值。由于该表是动态的,所以在页面回发到服务器时不会重新构建该表。如果希望重新构建表,则需要再次呈现它,并使用从请求中提取的值。形式重新填充它。

HTML Markup

HTML标记

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

#2


1  

You need to add a TableCell to the TableRow.Cells collection, and add the TextBox to the TableCell.Controls collection:

您需要在TableRow中添加一个TableCell。单元格集合,并将文本框添加到TableCell中。控制集合:

TableCell cell = new TableCell();
cell.Controls  = new TextBox();
tblRow[j].Cells.Add(cell);
editDataTable.Rows.Add(tblRow[j]);

The most direct way to access the textboxes is to keep them all in a list:

访问文本框最直接的方式是把它们都放在一个列表中:

List<TextBox> textBoxes = new List<TextBox>();

and replace cell.Controls = new TextBox(); above with this:

和更换电池。控制= new文本框();上面这个:

TextBox tb = new TextBox();
textBoxes.Add(tb);
cell.Controls = tb;

And then you can iterate through the textBoxes afterward without having to search for them in a tree.

然后你可以遍历文本框,而不必在树中搜索它们。