在使用MasterPage的Web窗体中FindControl

     当页面是使用MasterPage的Web窗体时,Page.FindControl返回的总是null,因为要查找的控件的NamingContainer已经不是单纯的Page控件了

而是当前Web窗体所要查找的控件所在的ContentPlaceHolder控件,所以可以用以下方法查找:

Page.Master.FindControl("MainContent").FindControl("TextBox1"as TextBox

你可以在页面上通过设置Trace="true",可以清楚查看整个页面的控件树结构。

更多的FindControl可以参考http://www.odetocode.com/Articles/116.aspx

原文地址:https://www.cnblogs.com/_dragon/p/2206591.html