简体   繁体   English

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

[英]How to find a control recursively in ASP.NET

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). 在Page_Load上,表中填充了许多行和多列(我很自豪,我认为这样)。 Inside some of the table cells there is a <asp:TextBox /> . 在一些表格单元格中有一个<asp:TextBox />

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. 就我而言,我的表名为editDataTable。 So in my code where I add the Rows, I've added the control too, as follows. 所以在我添加Rows的代码中,我也添加了控件,如下所示。

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

You need to add a TableCell to the TableRow.Cells collection, and add the TextBox to the TableCell.Controls collection: 您需要将TableCell添加到TableRow.Cells集合,并将TextBox添加到TableCell.Controls集合:

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(); 并替换cell.Controls = new TextBox(); above with this: 上面这个:

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. 然后,您可以在之后迭代textBoxes ,而无需在树中搜索它们。

Here is an example of building a dynamic table in ASP.NET during PageLoad, and reading in the values through a postback. 下面是在PageLoad期间在ASP.NET中构建动态表,并通过回发读取值的示例。 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. 如果要重建表,则需要再次呈现它并使用从Request.Form中提取的值来重新填充它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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