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

发布于 2024-11-27 20:57:49 字数 1346 浏览 1 评论 0原文

有很多关于这方面的例子,而且我非常了解使用递归来查找控件的概念。一旦在回发中找到控件,就可以与之交互,等等。

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

<asp:Table ID="editDataTable" runat="server">
</asp:Table>

在 Page_Load 上,该表填充了许多行和许多列(我很自豪我发现那个出来)。在某些表格单元格内有一个

你已经猜到了,我需要获取这些文本框的值!

(我已经得到了递归代码并检查了它,看起来不错。)

我的表有两列。左侧包含“公司名称、电话”等标题,右侧一列包含带有相应标题值的文本框。因此,用户可以编辑文本框(例如,如果电话号码发生更改)并提交更改。

显然,行是根据用户动态添加的。

我遇到的问题是:填充表格时需要将控件添加到页面。大致如下:

myTable.Control.Add(new TextBox());

在我的例子中,我的表称为 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
}

那些清醒的人会知道您无法将文本框控件添加到表格中!

最后,我的问题是:

  • 如何在表格中添加文本框的控件?
  • 我在哪里添加它们?
  • 是否有任何额外的建议可以帮助我完成检索动态添加的文本框的文本值的任务?

这是我的递归代码,以防您好奇:

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

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:

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

太傻旳人生 2024-12-04 20:57:49

下面是在 PageLoad 期间在 ASP.NET 中构建动态表并通过回发读取值的示例。由于该表是动态的,因此当页面回发到服务器时,它不会被重建。如果要重建表,则需要再次呈现它并使用从 Request.Form 中提取的值来重新填充它。

HTML 标记

    <asp:Table ID="editDataTable" runat="server">
    </asp:Table>
    <asp:Button runat="server" Text="Submit" OnClick="Submit_Click" />

代码标记

    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>  ::  " + Request.Form[i] + "</p>");
            }
        }
    }

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.

HTML Markup

    <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>  ::  " + Request.Form[i] + "</p>");
            }
        }
    }
甜扑 2024-12-04 20:57:49

您需要将 TableCell 添加到 TableRow.Cells 集合,并将 TextBox 添加到 TableCell.Controls集合:

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

访问文本框的最直接方法是将它们全部保存在列表中:

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

并将上面的 cell.Controls = new TextBox(); 替换为:

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

然后您可以迭代 textBoxes 之后没有必须在树上寻找它们。

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

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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文