views:

85

answers:

2

I have an asp.net content page which is used inside of a master page (with header, menu and some links). I would like to reuse it in a different context without the master page (to not display the header and menu there), or with an empty master page if this is somehow possible. I don't want to violate DRY principle by taking the whole page and creating a standalone clone of it for obvious reasons. Is this somehow possible ?

+4  A: 

How about wrapping-up the shared content in a user control?

A user control is a kind of composite control that works much like an ASP.NET Web page—you can add existing Web server controls and markup to a user control, and define properties and methods for the control. You can then embed them in ASP.NET Web pages, where they act as a unit.

LukeH
Sounds reasonable, I'll probably go this way, thanks. The only problem is that I have to transform the already existing page to a control, but hopefully that's not too much work..
Thomas Wanner
+3  A: 

Yes, you can set the master page dynamically in the content pages Page_PreInit method:

private void Page_PreInit(object sender, EventArgs e)
{
    this.MasterPageFile = "MyMasterPage.master"
}

Set up some logic to dynamically choose which master page filename to pass in, and you are now sharing one content page with many master pages.

Moo
I was thinking the same thing! Then there's no need to convert existing pages to a control.
Pieter Nijs