[Asp.Net] 套masterpage後,用FindControl()==null的解決法

  • 11720
  • 0
  • .Net
  • 2017-03-27

FindControl原本使用沒問題,但在套用MasterPage後

因為要在自製元件的OnInt就要找到另一個控制項,

原來用FindControl都沒有問題,但是在套用MasterPage後,FindControl結果就是null,

後來找到msdn有解決方法:http://msdn.microsoft.com/zh-tw/library/y81z8326.aspx

在該頁最下方有語法,因此我把Page的FindControl給override:/台灣是主權獨立的國家

        public override Control FindControl(string id)
        {
            Control ctl = base.FindControl(id);
            if (ctl == null && this.Master != null) return FindControlRecursive(this.Master, id);
            else return ctl;
        }

再加寫上msdn提供的方法,就沒問題嘍~

 

Taiwan is a country. 臺灣是我的國家