views:

33

answers:

2

I have two master pages in my project, one for web views and another for emails. In the web master page is a placeholder to put page specific JavaScript, if a page is using this and I try to programmatically change the masterpage to send as an email the server falls over as it can't find the placeholder.

Is there a way to change the behaviour so that if a placeholder can't be found it just doesn't include that bit? I'm thinking maybe on my page base class looping over a collection of placeholders and content tags and removing the ones that don't match up, but I'm not sure if there's a good point in the page lifecycle to do this.

I'm using MVC not WebForms if this makes a difference.

A: 

You can't have a Content control with a ContentPlaceHolderID that doesn't exist.

Instead of taking the ContentPlaceHolder out, why don't you just provide an empty Content control? It'll just render nothing.

womp
Maybe I explained it badly, I want to have a non-empty Content control that will render on the web master page, but not on the mail master page
Chao
If you're reusing pages across multiple master pages, all the master pages will have to have ContentPlaceHolders for all the possible content controls. However, each masterpage is free to set the visibility of anything added to it's placeholders to "false". So in your mail master page, you could just do a javascriptPlaceHolder.Controls[0].Visible = false.
womp
+1  A: 

Add the ContentPlaceHolder into the mail masterpage and set the visible property to false and the content from the page for that placeholder won't render.

<asp:ContentPlaceHolder id="javascriptPlaceHoldereHolder1" runat="server" Visible="false" />
Mike J