Ads

Wednesday, 3 April 2013

Hide Content Place Holder Programmatically

Lets assume the case where we want to replace a control from the master page by a custom web part. Initially we commented everything inside the content place holder and added my web part.

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.

No comments:

Post a Comment

Ads