在带有母版页的页面上查找控件
我必须在绑定到母版页的 aspx 页面中找到一个 Control
。
母版页包含:
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
内容页包含:
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
</asp:Content>
我添加了一个带有 ID
formtable
的 Table
作为 Content2
的子项。
我尝试使用以下代码访问Table
,但代码返回null
:
protected void Ok_Click(object sender, EventArgs e)
{
Table tblForm = this.FindControl("MainContent").FindControl("formtable") as Table;
}
如何访问Table
?
I have to find a Control
in an aspx page bound to a master page.
The master page contains:
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
The content page contains:
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
</asp:Content>
I added a Table
with ID
formtable
as a child of Content2
.
I tried to use the following code to access the Table
, but the code returns null
:
protected void Ok_Click(object sender, EventArgs e)
{
Table tblForm = this.FindControl("MainContent").FindControl("formtable") as Table;
}
How can I access the Table
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个
查看这个 内容中的控件 ID 命名页面了解更多详情
Try this
Checkout this Control ID Naming in Content Pages for more details
当您尝试执行此操作时,您处于什么环境中?您是否在单个页面的代码隐藏中?
如果是的话,应该是
Content1.FindControl("formtable") as Table
就这样了。What context are you in when you are trying to do this? Are you in the codebehind of the individual page?
If you are it should be
Content1.FindControl("formtable") as Table
and that would be it.使用 findControl() 有时会导致复杂化。
在母版页中为该控件定义公共属性,然后通过该属性访问控件会更容易。
您应该在子页面中添加此行:
Working with findControl() cause complications sometimes.
It is easier to define a public property for that control in master page and then access control through the property.
you should add this line in child page: