Hide or Show if a field is populated

Jai K asked on March 25, 2020 16:10


I am displaying the document name in an ad-hoc layout like this

<%= CurrentDocument.DocumentName %>

But the document is of type custom page type which has a field called "Title" and instead of documentName I want to display the title from that custom field?

I only want to display the title if the title is populated. how do I do this?

Recent Answers

Dmitry Bastron answered on March 25, 2020 16:35

Hi Jai,

You can do something like this:

<%= string.IsNullOrWhiteSpace(CurrentDocument.GetValue("Title")) ? CurrentDocument.DocumentName : CurrentDocument.GetValue("Title") %>
0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on March 26, 2020 08:32

I would rather recommend switching from ASCX layout to HTML layout and use macros. Uing the code blocks <%...%>, displaying expressions may lead to errors - the may depend on the page and control life cycle so it may get broken if you add or reorder web parts or controls on the page. This approach is also obsolete (used in ASP.Net 1.1) and not a best practice....
Another option would be adding e.g. a static text web part to the template and use the macro in it to get the desired value.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.