简体   繁体   English

如何通过id在页面上找到控件

[英]How to find control on page by id

Is there a simple way to find a control in ASP.NET by id (in any nested container)? 是否有一种通过id(在任何嵌套容器中)在ASP.NET中查找控件的简单方法? Other than traversing whole controls tree. 除了遍历整个控件树。

Something like this example: 像这样的例子:

TextBox tb = new TextBox() { ID = "textboxId"};
panel3.Controls.Add(tb);

And in other method/class: 在其他方法/类中:

 TextBox nameTextbox = MethodToFindControl("textboxId") as TextBox;

If I understand your question correctly, you could use method FindControl as follows: 如果我正确理解您的问题,您可以使用FindControl方法,如下所示:

TextBox nameTextbox = (TextBox) panel3.FindControl("textboxId") ; TextBox nameTextbox =(TextBox)panel3.FindControl(“textboxId”);

不...你必须遍历所有控制树,直到找到控件然后你停止遍历,你的方法(MethodToFindControl)应该是一个递归方法,它接受两个参数:根容器(大部分时间是页面)和要查找的控件的ID。

In case you are not able to find the control using findcontrol declare corresponding td/panel as server control and then you should be able to easily find the required control within td or panel element. 如果您无法使用findcontrol声明相应的td / panel作为服务器控件找到控件,那么您应该能够在td或panel元素中轻松找到所需的控件。

DirectCast(tdMultiCnstrTypCode.FindControl("BBIMultiConsCode" & cnt + 1), DropDownList)

This approach is easier than traversing though all controls and child controls of the page. 这种方法比遍历页面的所有控件和子控件更容易。

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

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