用了母版后 FindControl 找不到控件的解决办法

作者:vkvi 来源:ITPOW(原创) 日期:2014-12-14

平时在页面中用 Page.FindControl 时都可以找到控件,但是在使用了母版后,页面中 FindControl 就找不到控件了,结果为 null。

怎么解决?

Page.Master.FindControl("ContentPlaceHolder1").FindControl("_code") as TextBox)

得用 Page.Master 先回到母版,再从母版找下来,其中 ContentPlaceHolder1 为母版中 <asp:ContentPlaceHolder 控件,并不是在页面中的 <asp:Content 控件。

说明:虽然直接 FindControl 是找不到的,但是循环 Controls 再比较 Id,却是另一个解决办法。

相关文章