Tuesday, July 20, 2010

Hide Content Place Holder Programmatically !

I was working on replacing a control from the master page by a custom web part. Initially i commented everything inside the content place holder and added my web part. If you want to know how to add a web part in the master page, have a look at my previous post at :


Later on i moved the web part reference out of the content place holder and made the visible attribute of the content place holder to false.

But now the requirement is to dynamically turn it ON and OFF (i.e through code)

Lets follow the steps given below to achieve this:

1. Go to the page_load method of the web part.
2. Write the following lines of code.

if (!Page.IsPostBack)
ContentPlaceHolder contPlcHolder = (ContentPlaceHolder)Page.Master.FindControl("PlaceHolderGlobalNavigation");
contPlcHolder.Visible = false;

//Rest of the code


If there is any nested content place holder then append that number of FindControl("ControlName") to the above code where we are finding the control.

That's it. Save the code and deploy the latest web part code.

No comments:

Post a Comment