Use default ContentPlaceHolder content instead of page content

If the page using the master page does not have an asp: Content control for one of the master ContentPlaceHolders pages, the default content is displayed instead. I want to use the default content of the page on which there is , there asp: Content control for that ContentPlaceHolder.

On a page using the master page, I want to decide whether to use the default content or the page content. How to show default content from master page instead of content from asp: Content control for ContentPlaceHolderID?

For example, let's say I have a ContentPlaceHolder for a menu. The default content shows the main menu. The page builds the menu from the query, but if there is no result for the query, I want to show the default menu. By default, an empty asp: Content control will be displayed. How do I get the content of the default home page?

+2


source to share


3 answers


The best approach I've come up with is not to use the default content in the ContentPlaceHolder. Instead, I added the default content to the PlaceHolder next to the ContentPlaceHolder:

<asp:ContentPlaceHolder ID="Menu" runat="server" /><!-- no default content -->
<asp:PlaceHolder runat=server ID="DefaultContentForMenu" Visible=false
    EnableViewState=false >Default menu content here</asp:PlaceHolder>

      

Then I added the ForceDefaultContentForMenu property to the master page so that pages using the master page can specify that the default content should be used even if the page contains its own content.

The Render master method shows default content if ForceDefaultContentForMenu property is true or content placeholder is empty:



protected override void Render(HtmlTextWriter writer)
{
    if (ForceDefaultContentForMenu || !Menu.HasControls())
    {
        Menu.Controls.Clear();
        DefaultContentForMenu.Visible = true;
    }
    base.Render(writer);
}

      

Pages using the master page will now receive default content unless they add their own content for the menu content placeholder, but can specify that the default content should be used instead of their own content.

The only drawback I've found with this approach is that when Visual Studio adds a content area to the page, the content is not copied by default. For the job I am doing, this is an advantage, not a disadvantage, as if I add a content area to the page, because I don't need the default content.

+1


source


One way to do this is to clear the collection of placeholder content controls in Page_Load and add an updated menu.



protected void Page_Load(object sender, EventArgs e)
{
    if(needsBetterMenu)
    {
        ContentPlaceHolder holder = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
        holder.Controls.Clear();
        holder.Controls.Add(betterMenu);
    }
}

      

+3


source


If your tone is with changing the content of the master page in the code behind the child page, you can do the following:

Add runat = "server" to the html control on the master page you want to change:

Site.Master

...
<ul id="menu" runat="server">
    <li><a href="#">Link 1</a></li>
    <li><a href="#">Link 2</a></li>
</ul>
...

      

Then, in the code behind the child page that needs to change the menu content, enter the following code:

protected void Page_Load(object sender, EventArgs e)
    {
        HtmlGenericControl c = Master.FindControl("menu") as HtmlGenericControl;

        if (c != null)
        {
            c.Controls.Clear();
            c.Controls.Add(new HtmlGenericControl("li") { InnerHtml = "<a href=\"#\">Link 3</a>" });
        }
    }

      

or whatever HTML you want to put in the menu control.

The page without code outputs the following html:

<ul id="ctl00_menu">
    <li><a href="#">Link 1</a></li>
    <li><a href="#">Link 2</a></li>
</ul>

      

and a page with code with the following display:

<ul id="ctl00_menu">
    <li><a href="#">Link 3</a></li>
</ul>

      

Obviously, you don't want to use this code as it is. It was only a prototype. I would refactor it to add content to any control and throw it into the base page class that all my pages inherit.

0


source







All Articles