如何在 ASP.NET 中递归查找控件
有很多关于这方面的例子,而且我非常了解使用递归来查找控件的概念。一旦在回发中找到控件,就可以与之交互,等等。
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面是在 PageLoad 期间在 ASP.NET 中构建动态表并通过回发读取值的示例。由于该表是动态的,因此当页面回发到服务器时,它不会被重建。如果要重建表,则需要再次呈现它并使用从 Request.Form 中提取的值来重新填充它。
HTML 标记
代码标记
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
Code Markup
您需要将
TableCell
添加到TableRow.Cells
集合,并将TextBox
添加到TableCell.Controls
集合:访问文本框的最直接方法是将它们全部保存在列表中:
并将上面的
cell.Controls = new TextBox();
替换为:然后您可以迭代
textBoxes
之后没有必须在树上寻找它们。You need to add a
TableCell
to theTableRow.Cells
collection, and add theTextBox
to theTableCell.Controls
collection:The most direct way to access the textboxes is to keep them all in a list:
and replace
cell.Controls = new TextBox();
above with this:And then you can iterate through the
textBoxes
afterward without having to search for them in a tree.