Changing Master Page ContentPlaceHolder Contents From Content Pages

We always need to have dynamic pages for our website and very often we generally use templates for them. In master-content page concepts we have them already where master page have the layout of the site and content pages supplies dynamic contents to master page. But it's not possible to manage master page content place holders from the content pages code-behind for dynamic contents because content place holders are placed in master pages. So we need to create similar environment of master pages in content pages by creating WebControls such as ContentPlaceHolder, Label, TextBoxes etc.


To get controls of master pages we can use FindControl("ContenPlaceHolderID") and create similar WebControls in content pages code-behind so that we can manage controls easily. Suppose we want dynamic page header title in a webpage. The initial page url might be some thing like

http://somedomain.com/demo.aspx?action=add

and later we want to use the same page for edit function some thing like 

http://somedomain.com/demo.aspx?action=edit

and we want the header title to change from "Add New Customer Entry" to "Edit Customer Entry". So we must have to have the master page's content place holder in the code-behind of the content page (demo.aspx).

We may have the following code in master page for sending initial content of the page header title.


<asp:ContentPlaceHolder id="ContentHeaderTitle" runat="server"></asp:ContentPlaceHolder>

Now Content page need to have a content control to supply titles to master page and the code for this will:

<asp:Content runat="server" ContentPlaceHolderID="ContentHeaderTitle" ID="ttlForm"><asp:Label ID="lblContent" runat="server">Add New Customer Entity</asp:Label></asp:Content>

Now if we want to change the title from the code-behind of content page then the following code will help:

ContentPlaceHolder myContent = Master.FindControl("ContentSubHeaderTitle") as ContentPlaceHolder;
Label lblContent = myContent .FindControl("lblContent") as Label;
lblContent.Text = "Edit Customer Entity";

It's possible to find all other controls from master pages and use them in code behind using the same way.

Hope that helps. :)

Comments

Popular posts from this blog

Adding and posting custom field values using WHMCS API

Custom Twitter Feed in Webpage using Twitter API and Javascript