平时在页面中用 Page.FindControl 时都可以找到控件,但是在使用了母版后,页面中 FindControl 就找不到控件了,结果为 null。
怎么解决?
Page.Master.FindControl("ContentPlaceHolder1").FindControl("_code") as TextBox)
得用 Page.Master 先回到母版,再从母版找下来,其中 ContentPlaceHolder1 为母版中 <asp:ContentPlaceHolder 控件,并不是在页面中的 <asp:Content 控件。
说明:虽然直接 FindControl 是找不到的,但是循环 Controls 再比较 Id,却是另一个解决办法。