How to hide controls when blank in a Layout Page?
Hi All,
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
i.e.
<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>
<tr>
<td>
<PublishingWebControls:RichImageField FieldName="PublishingRollupImage" runat="server">
</PublishingWebControls:RichImageField></td>
</tr>
</end of condition>
</PublishingWebControls:EditModePanel>
a) One way: Use <WebPartPages
ataFormWebPart ... > <WebPartPages
PDataSource DataSourceMode="ListItem" ID="ds1" SelectCommand=""><SelectParameters>
<SharePointWebControls
ataFormParameter ..... ??? what to pass here ??? >
</SelectParameters>
</WebPartPages
PDataSource> 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 asp
laceholder, 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" ....>
<tr>
<td>
<SharePointWebControls:FieldValue id="Ctrl1........ ></....>'
<td>
<tr>
</MyConditionalPlaceHolder >
Would appreciate your suggestions.
Thanks,
Mo
Risposte
- Hi Everyone,
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 { 16 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
{
base.Render(writer);
}22 } 23 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 } 35 36 } 37
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" >
</custom:HideWhenBlank>
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.
Best Wishes,
Mo- Contrassegnato come rispostaMohan Taneja sabato 17 gennaio 2009 2.37
Tutte le risposte
- Same problem here.
I created a page layout from a content type... and I would like to hide a field when empty.
For example I have<strong>Date:</strong> <SharePointWebControls:TextField FieldName="Project_x0020_Date" runat="server"></SharePointWebControls:TextField>
When Project_x0020_Date is empty would be nice to hide also the heading "<strong>Date:</strong>" .
Any hints to do that?
Many thanks in advance
- A couple of ideas...
Have a look at the rendered page to get the ID(s) of the controls you want to hide and use some JavaScript to check the contents and hide the elements as needed.
Or
Add code to the page's OnPreRender or (better) add a WebControl and add code to its OnPreRender to examine the controls on the page and hide as needed. If you go the control route you could expose a property that let you specify the controls to check and hide.
HTH,
--Doug
http://www.elumenotion.com/blog My solution to this problem (although not a very pretty one) is to have alternating Page Layouts based on the same Content Type. In that way I can have "Article with image" and "Article without image".
You can at any time switch between the different layouts and no information is lost when going between them.
Pontus
Pontus HaglundIf I understand correctly you could create a custom base layout page with all the logic you require to hide / display information on that particular page layout.
For example
aspx page.
<%@ Page language="C#" Inherits="HHS.MOSS.SSEPublishingSite.SiteDefinition.CustomPageLayoutBase, HHS.MOSS.SSEPublishingSite.SiteDefinition, Version=1.0.0.0, Culture=neutral, PublicKey
Token=91e2a6d49fe103c0" %>
Codebehind cs filepublic class CustomPageLayoutBase: PublishingLayoutPage
{
protected PlaceHolder IntroPlaceHolder;
protected PlaceHolder ApproveNewsPromotionPlaceHolder;protected override void OnLoad(EventArgs e)
{
// Only show approvenewspromotion placeholder if not in display mode and current user belongs to sse member or owner group
if (ApproveNewsPromotionPlaceHolder != null)
ApproveNewsPromotionPlaceHolder.Visible = false; etc etc
Hope this helps- We had the same issue with MCMS 2002, the predecessor to WCM. Best approach IMHO was to create a custom server control that you wrapped the FC's in. It did a check for all it's child controls and if their content was empty/null, it removed them from the Controls collection.
-AC [MVP Office SharePoint Server] <> http://www.andrewconnell.com/blog - Hi Everyone,
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 { 16 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
{
base.Render(writer);
}22 } 23 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 } 35 36 } 37
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" >
</custom:HideWhenBlank>
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.
Best Wishes,
Mo- Contrassegnato come rispostaMohan Taneja sabato 17 gennaio 2009 2.37

