How to reference a Master Page from a user control?

前端 未结 4 2093
执念已碎
执念已碎 2021-02-07 19:01

I\'m looking for a way to (preferably) strongly type a master page from a user control which is found in a content page that uses the master page.

Sadly, you can\'t use

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-07 19:35

    Try Page.Master.

    Whatever whatev = (Whatever)Page.Master;
    

    You'll have to make sure you add the proper using statements to the top of your file, or qualify the Master page type inline.

    One potential gotcha is if this control is used by a different page whose master page is NOT the same type. This would only get caught at runtime.

提交回复
热议问题