如何找到ID页面上控制 [英] How to find control on page by id

查看:162
本文介绍了如何找到ID页面上控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种简单的方法来找到ID在ASP.NET控件(在任何嵌套容器)?除了遍历整个控制树。

事情是这样的例子:

 文本框TB =新的TextBox(){ID =textboxId};
panel3.Controls.Add(TB);

和其他方法/类:

 文本框nameTextbox = MethodToFindControl(textboxId)的文本框;


解决方案

没有......你,直到你找到了控制,那么你停止遍历遍历所有的控件树,你的方法(MethodToFindControl)应是一个递归的方法,该方法两个参数:根容器(大部分时间它在网页)和控制来寻找的id

Is there a simple way to find a control in ASP.NET by id (in any nested container)? 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;

解决方案

No ... You have to traverse all controls tree until you find the control then you stop traversing, your method (MethodToFindControl) shall be a recursive method that takes two arguments: the root container (most of the time its the page) and the id of the control to look for.

这篇关于如何找到ID页面上控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆