未找到儿童控制。 [英] Child control not found.
问题描述
Hello Friends,
我有一个嵌套在父控件中的子控件。
例如:
1>我使用了详细视图控件。
2>我想自定义寻呼机,所以我做了类似下面的事情:
Hello Friends,
I have a child control nested within a parent control.
For example:
1>I used a details view control.
2>I wanted to customize the pager, so i did something like below:
<asp:DetailsView ID="dv" runat="server">
<PagerTemplate>
<asp:CheckBoxList ID="cbl" ToolTip="hi" runat="server" />
</PagerTemplate>
</asp:DetailsView>
现在我想访问CheckBoxList按钮单击的事件处理程序。所以在按钮的事件处理程序中,我做了:
Now i wanted to access the CheckBoxList in a button click's event handler. So in the event handler of the button, i did:
CheckBoxList cbl = (CheckBoxList)(dv.FindControl("cbl"));
这里我应用了一个断点,我发现cbl原来是空。
我又尝试了一件事:
here i applied a break point, i observed that "cbl" turned out to be "null".
I tried one more thing:
foreach (Control c in dv.Controls)
{
if (c is CheckBoxList)
{
Response.Write("true");
}
}
再次我在
again i applied a break point on
Response.Write("true");
这是在if内,它不会执行。
然后我试过:
this is within the "if", it does not execute.
then i tried:
CheckBoxList cbl = (CheckBoxList)dv.BottomPagerRow.Cells[0].FindControl("cbl");
再次cbl为空。
最后,我试过了:
again "cbl" is null.
finally, i tried:
CheckBoxList cbl = (CheckBoxList)Page.FindControl("cbl");
cbl再次为空。
我不明白,为什么那个控件空?
有什么建议?
谢谢和问候,
Rahul
"cbl" is null again.
I cant understand, why is that control "null"?
any suggestions?
Thanks and regards,
Rahul
推荐答案
这是范围问题搜索。请参阅: http://msdn.microsoft.com/en-us /library/31hxzsdw%28v=vs.110%29.aspx [ ^ ]。
It's the matter of the scope of the search. Please see: http://msdn.microsoft.com/en-us/library/31hxzsdw%28v=vs.110%29.aspx[^].
方法搜索只有页面的直接或顶级容器;它不会递归地搜索页面中包含的命名容器中的控件。要访问从属命名容器中的控件,请调用该容器的FindControl方法。
The method searches only the page's immediate, or top-level, container; it does not recursively search for controls in naming containers contained on the page. To access controls in a subordinate naming container, call the FindControl method of that container.
有关 Control.FindControl
的信息。以下是您可以执行的操作:如果您只有搜索的顶级上下文,则某些 Page
:获取其所有控件:http://msdn.microsoft.com/en-us/library/system.web .ui.control.controls%28v = vs.110%29.aspx [ ^ ]。
然后,对于每个控件,使用它你可以使用上面引用的相同 Control
属性作为页面也是控制
。递归地执行:
Thes same is told about Control.FindControl
. Here is what you can do: if your have only the top context of the search, some Page
: get all its controls: http://msdn.microsoft.com/en-us/library/system.web.ui.control.controls%28v=vs.110%29.aspx[^].
Then, for each control, use its you can use the same Control
property referenced above, as the page is also Control
. Do it recursively:
class SomeType {
static Control FindControl(Control control, string id) {
if (control.ID == id) return control;
if (control.FindControl(id) != null) return control;
foreach(Control child in control.Controls) {
Control foundControl = child.FindControl(id);
if (foundControl != nill)
return foundControl;
}
return null;
}
//...
void SomeMethod() {
Control requiredControl = FindControl(Page, "cbl");
}
} //class SomeType
祝你好运,
这篇关于未找到儿童控制。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!