Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

How to hide controls when blank in a Layout Page?

  Asked By: Ramsey    Date: Jan 19    Category: Sharepoint    Views: 3667

I want to hide a field (image in this case but could be any field) in a Page Layout when it is blank. This field is inside a table td tag


<PublishingWebControls:EditModePanel PageDisplayMode="Display" runat="server" Wrap="false" ID="disp" Width="734px">

<what could be the tag here to display this conditionally i.e. only if user selected a image it should display>

<PublishingWebControls:RichImageField FieldName="PublishingRollupImage" runat="server">



</end of condition>


a) One way: Use <WebPartPagesataFormWebPart ... >

<WebPartPagesPDataSource DataSourceMode="ListItem" ID="ds1" SelectCommand="">

<SharePointWebControlsataFormParameter ..... ??? what to pass here ??? >



i.e. how to refer to the current list Item in the DataFormWebPart ?

b) Other way I can think of is to create a custom control inherited from asplaceholder, override the visible property, create a new property as controlsToCheck="ctrl1" and check the value of this control "ctrl1" and if it is blank set the visible property to false otherwise true.

(I have not developed this this but probably it should work)

On the pagelayout use the controls the following way:

<MyConditionalPlaceHolder .... ControlToCheck="Ctrl1" ....>



<SharePointWebControls:FieldValue id="Ctrl1........ ></....>'



</MyConditionalPlaceHolder >

Would appreciate your suggestions.



1 Answer Found

Answer #1    Answered By: Tatiana Houston     Answered On: Jan 19

1 [CLSCompliant(false)]
2 [ParseChildren(false)]
3 [Guid("please put a guid here")]
4 public class CustomTrimming : Microsoft.SharePoint.WebControls.SPSecurityTrimmedControl
5 {
6 public string FieldNameToCheck {get;set;}
7 protected override  void Render(HtmlTextWriter writer)
8 {
9 SPListItem item  = null;
if (!DesignMode)


10 if (SPContext.Current != null)
11 {
12 item = SPContext.Current.ListItem;
13 }
14 if ((FieldNameToCheck != string.Empty) && (item != null))
15 {
17 if (CheckIfNullOrBlank(item[FieldNameToCheck]) == false)
18 {
19 base.Render(writer);
20 }
21 }
else // to display the nested controls  / html when in SharePoint Designer 2007


22 }
24 private bool CheckIfNullOrBlank(object data)
25 {
26 if (data == null || (Convert.ToString(data).Trim() == string.Empty))
27 {
28 return true;
29 }
30 else
31 {
32 return false;
33 }
34 }
36 }

First of all I appreciate and thanks everyone to post your responses. Am coming back to this post after a long time,
to post the solution I had used.

Please compile the above code and check  for typos since I extracted this from a code doing other things as well. From within the page  using SPContext we have access to the data for the same page, hence we can check its value and decide to either render or not render the nested control  contents.

Here is how usage would look like:

<custom:HideWhenBlank runat="server" FieldNameToCheck="ImageFieldHere">
.. <...... Field="ImageFieldHere" >


so the value of "ImageFieldHere" will be checked before displaying the image  and if it is blank, all the HTML and publishing controls inside  the <custom:HideWhenBlank> tags will not be parsed and vice versa, i.e. if the image does exist it will be shown.
Also as a note 1) this will work  for all data types and not just image types.
2) It is not necessary to inherit from SPSecurityTrimmedControl and you could use other UI base control.
3) In SPD in design mode you will continue to see the nested controls (a check made in above code for design mode), since we would not have access to SPContext at that time. We can the see the end result when the page is shown in browser.

I used the above is SPD 2007 and it worked well.

Didn't find what you were looking for? Find more on How to hide controls when blank in a Layout Page? Or get search suggestion and latest updates.