Add a master page programmatically - asp.net

First off, I know this isn't a normal thing to do, so if it isn't possible, just let me know.
I have a bunch of pages (100+) that are all designed to be contained within an iframe. Recently, I've decided to move away from this and use a master page to do some formatting on the page that used to contain the iframe. However, in order to do this, the page inside the frame is now going to be the page that is navigated to. What I need to be able to do is apply this new master page to a page that didn't have a master page prior and I'd like to do it without having to change the markup of all the other pages. All of these pages inherit from a single parent class that can be used to accomplish this, if it is possible. It is necessary that iframes are not used in this solution, as their compatibility is part of the problem I'm trying to work around. Thanks for any help/ideas you can offer.
EDIT
This is the error I'm getting when I try the way the comments suggest:
Content controls have to be top-level controls in a content page or a nested master page that references a master page.

Should be very possible. As I recall, you can just have your base class set its own MasterPage in the OnPreInit event:
this.MasterPage = "~/SpecificMasterPage.aspx";
Response to comments
That complicates things. Here's something you can try, but I don't know whether it will work: try creating regular page to be your "master" page, and loading the other pages in as if they were controls using the Page.LoadControl method. I don't know whether whole pages can be loaded this way, and even if they are this is hacky at best, since you'll be producing new <html>, <head>, and <body> tags within the body of the page.
So your best bet is probably to convert all your pages to either use a MasterPage or to be Controls. If you're clever, you might be able to write a little program that can do this to all your pages automatically.

Related

ASP.NET master page - control if menu html is shown or not

I'm creating an ASP.NET site where some pages need to have a div floated to the left with the site menu in and other pages don't have the menu div and instead the content takes up the entire page width.
I'm planning to use master pages - I was wondering how best to achieve this - a few immediate thoughts spring to mind:
Create 2 master pages, one with menu and one without. When creating content pages choose which master to inherit from. This is straightforward, but may require more rework if the site design changes
Create a single master page with a content placeholder for the menu. On each page include a menu div only if it is needed (seems more messy this approach)
Nested master pages?
It seems like this may be a common problem, so interested in how this is normally addressed. Note I am questioning the need for having pages without the menu - it would be simpler if all pages had exactly the same structure.
Nested master pages is probably the standard answer and should work well.
An alternative approach that occurs to me would be to put your menu in a panel control on the master page and expose a property that lets you toggle the visibility of the panel. I have not tried this yet, so I am not sure how practical it is, but it might be a fun concept to play with. If it works the way I expect this gives you a single master page that can be reconfigured by the content page.
It's up to personal preferences, but the way I was recommended was to create nested master pages,
the first master page would not contain the menu but the 2nd one would and then just point to one of the master pages.
Nested Master pages should solve your problem and also keep the Web Site scalable.
At my previous company we used two separate master pages for a similar scenario. One that included the menu and one that did not. And because they both inherited the same base master page, they both had the same functionality.
But then again, I think it comes down to personal preference.
Personally, I like the idea of having two separate pages as that way it's a bit easier to see what's going on, compared to the nested master pages.

how to include aspx page in another aspx page? in asp.net

I'm working on a custom web application and because I have many separate blocks I want to put them on a different page, I was wondering how can I include aspx content on existing page?
the reason I'm asking about aspx, and not custom web controls, is because I planned to use pagemethods on those separate pages.
Another reason is that I saw someone some saying we can do that, can't find the link now, so thought I'll try too, since it looks very neat:)
Thanks
Server.Execute (secondUrl) might do what you want. But UserControls are definitely more ASP.Net-ish :).
Based on what you describe, the proper answer is custom controls as they allow you to place the code anywhere on any page in an easily reusable manner.
Can you explain what you mean by "aspx content" - is that just html, is it controls? Also, what Page methods are you referring to?

Adding a ContentPlaceHolder to site.master

Apologies for the terribly newb question. We're currently implementing Google Web Optimizer in our ASP.NET Web Application and some of the code is supposed to go in very specific places on certain pages. For example, for the "Control Page" Google has some Javascript that sits outside of the <html> tags.
I know I probably don't need to place the code exactly where Google recommends, but we've been getting some goofy results lately, and I really wanted to make it as watertight as possible to ensure it's not just bad implementation.
We have a lot of files in our project that reference the site.master, but only one needs to have some Javascript placed outside the <html> tag.
This, in theory, seems simple enough, my question is this: Do I need to put a ContentPlaceHolder in every file that references the site.master? (Even the tens that aren't passing any code to the site.master?) That's not something I feel like doing for many different reasons (altering tens of files). If that's the case, and I do need to add empty ContectPlaceHolders to every page, is there some other way around things without having to piece together a unique file just to put some Javascript outside of the <html> tag?
Thanks for any help.
The answer is no, as long as the ContentPlaceHolder in the Master is empty. That way, you only put Content for that ContentPlaceHolder on the pages where you want something rendered.
The simple question is that you not need to place Blank pace holder.
If the master page find this place holder, then its replace the content on it, if not finds it then its leave what have inside.

Implementing a 'reverse' or 'nested' master page with ASP.NET

I'm hoping my cryptic title isn't too cryptic, but I'll try to explain what it is I actually want to do.
I have a master page 'A' which has child pages 'B' and 'C'.
This is implemented with the standard master page model in ASP.NET just fine.
My site has two themes 'T1' and 'T2'.
They actually represent different partners, T1 being our own company and T2 is a partner.
I want to display specific things on pages 'B' and 'C' depending upon which theme I'm using. For instance I may want to insert additional buttons or graphics on page 'B' specific to partner for theme 'T2'. This content would be at an arbitrary point on the page depending upon the page itself. Some partners may not even use this feature.
The problem is I want to do this as declaratively as possible, and minimize the knowledge that the pages have about the theme.
This is where the 'reverse' master page concept comes in. I want to define an area on a child page into which 'theme' specific content can be inserted. (I'm saying 'theme' because thats what ASP.NET uses - and most likely what I'd tie the content to).
So you may be wondering :
Why can't you just use a master page for this and add extra content sections? Reason is that the content may appear anywhere on the page in a location that is specific to that page.
Why can't you use a 'nested' master page? Essentially the same reason.
Possible solution:
I'm wondering about creating some kind of user control that would have a textual key representing what type of content would be inserted there. The user control would have to know what to display for each 'theme' - probably by dynamically creating the relevant additional user control.
This seems a little clumsy - so I'm wondering what kind of solutions others may have created for similar situations.
you can use CSS to position your content sections anywhere on your page. so i wouldnt worry about page locations, etc.
if it were me, i'd just dynamically load the 2 different css files into the one masterpage and based on the same logic render the different content into the placeholders.
I'm currently working on a site that has similar requirements that are too much for CSS. In one layout the login is horizontal towards the top of the render order, and in another its in the right hand column.
We're using standard ASP.NET themes to push out CSS, logo URLs via SkinID, etc.
To obtain the custom layout I've created a "Loader" control that loads UserControls via a list specified in a Loader attribute. The attribute can be defined directly in the loader markup, or via a .skin file in the theme.
Each key in the list is just the base name of a UserControl (I add the path and extension). The controls are created via Page.LoadControl().
Do you have fix layout for different partners ? Or do you want the partners to choose the layouts on runtime ?
If the layouts are fixed than 1 way is to create zen style CSS (http://www.csszengarden.com/) and than dynamically load it as per the client/partner. If you want the partners to modify the layouts on runtime than probably you might want to use Webparts.
Hope this helps.
You CAN use nested master pages in VS2008!
See here --
Scott Guthrie's Blog

Is it better to use Nested Master pages or User Controls for multi-sectioned website?

I am creating a website that has 5 different sub-sites each with the same overall look, but each has a different logo and added navigation.
What do you feel is a better approach:
Use the new nested master page capabilities to create 5 sub-master pages.
Use a single master page and dynamically load user controls corresponding to each sub-site's
specific logo/navigation.
I'm currently going down the nested master page route.
Update (12/19/2008):
I am using Nested Master pages, and it is working out great. You are able to pass-through ContentPlaceHolders, and even have default content for the placeholders.
Nested Master is much better than loading a user control dynamically. Dynamically loading a control is slow.
A MasterPage is a control, which could also be loaded dynamically. However, to answer your question, it sounds like different masterpages are the way to go. It will give you much more flexibility, especially if say, one client wants their logo running down the side or something that a user control would not be able for. Master will give you full control over wrapping html.

Resources