How to reference a Master Page from a user control?

前端 未结 4 2138
执念已碎
执念已碎 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条回答
  •  迷失自我
    2021-02-07 19:25

    The best way to do it that I've found is actually to build a custom class that is based off of UserControl, give it a Master property with a get accessor that fishes through the this.Page.Parent until it stops encountering master pages (If you are nesting, this step is unnecessary otherwise) and then return that web control as the type of the master page you want to use. Then, when you add a new user control, change it's base class to the name of your custom class. The .Master property will be accessible and cast properly as the master page you want it to use.

提交回复
热议问题