查找类控制 - 找不到控制 [英] Find control in class -- cannot find control
问题描述
我有一个ajax标签容器的aspx页面。在一类,我想找到的选项卡容器传递一些值。
I have an aspx page with an ajax tab container. In a class I want to find the tab container to pass some values.
我定义 MYPAGE
:
Page myPage = (Page)HttpContext.Current.Handler;
当在这个 MYPAGE
详细信息寻求通过点击添加观察它上市,我在寻找标签的容器。然而,当我定义我的选项卡容器
When looking in more details on this myPage
by clicking add watch it is listing the tab container I am looking for. However when I define my tab container
AjaxControlToolkit.TabContainer Workflow_TabContainer = null;
Workflow_TabContainer =
(AjaxControlToolkit.TabContainer)myPage.FindControl("Workflow_TabContainer")
as AjaxControlToolkit.TabContainer;
或
AjaxControlToolkit.TabContainer Workflow_TabContainer
(AjaxControlToolkit.TabContainer)myPage.FindControl("Workflow_TabContainer");
它没有找到的标签的容器。我也试图首先定义页,比的ContentPlaceHolder和搜索的占位符标签的容器。同样的问题。
it does not find the tab container. I also tried to first define the page, than the ContentPlaceholder and searched for the tab container in the place holder. Same issue.
任何帮助和/或提示是多少AP preciated。
Any help and/or hint is much appreciated.
感谢
推荐答案
的的FindControl
方法仅会在当前控制儿童。
The FindControl
method only looks in the current control for children.
如果你不知道在页面层次结构中的控件,你需要做一个递归搜索 - 这很可能是,如果你使用的是模板化控件,如 TabContainer的
。
If you don't know where in the page hierarchy the controls are, you'll need to do a recursive search - which is likely if you're using a templated control such as the TabContainer
.
由于我已为previously到<一个href=\"http://stackoverflow.com/questions/6525804/using-findcontrol-to-get-gridview-in-a-content-page/6527866#6527866\">similar回答:
As I've posted previously to a similar answer:
private Control FindControlRecursive(Control rootControl, string controlID)
{
if (rootControl.ID == controlID) {
return rootControl;
}
foreach (Control controlToSearch in rootControl.Controls)
{
Control controlToReturn =
FindControlRecursive(controlToSearch, controlID);
if (controlToReturn != null) {
return controlToReturn;
}
}
return null;
}
一旦你得到了你的控制,你应该使用它转换为
,然后就在这不是很是你所期待的情况下检查空:
Once you've got your control, you should cast it using as
and then check for null just in case it's not quite what you were expecting:
var tabContainer = FindControlRecursively(myPage, "Workflow_TabContainer")
as AjaxControlToolkit.TabContainer
if (null != tabContainer) {
// Do Stuff
}
这篇关于查找类控制 - 找不到控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!