2

When working with markup if I want to include some content conditionally, I use a placeholder in a normal way:

<asp:Placeholder Visible=<%# IsExpired %>
  <span>Prolong your subscription</span>
</asp:PlaceHolder>

Also I can use if-directive:

<% if(IsExpired) {%>
  <span>Prolong your subscription</span>
<% }%>

I prefer using the first one just because it does not make my markup messy. And what's the best way to conditionally include content? From the performance point of view, are they similar?

myroman
  • 561
  • 1
  • 5
  • 11
  • 1
    They are similar but different. There are not best way, use what is do your work. The first is more UpdatePanel friendly. The second add more code on aspx page and not on code behind. The first have additional attributes and you can add dynamically other controls inside the PlaceHolder. – Aristos Feb 12 '14 at 06:59

3 Answers3

2

native HTML tags are always faster than rendering server controls as there is no time spent for rendering them

Dmitry Pavlov
  • 30,789
  • 8
  • 97
  • 121
1

I think there is hardly anything to do with performance whether way you choose here. But actually you may use the following code:

<asp:Label runat="server" Visible=<%# IsExpired %>
  Prolong your subscription</asp:Label>

instead the other two. This could make it look more straight-forward.

Johnny
  • 481
  • 4
  • 13
  • Agree with you, but only for this example. Imagine there're more controls inside. – myroman Feb 12 '14 at 07:43
  • In that case, I personally prefer using PlaceHolder. But I always insert controls via code behind so I can control when to do the rendering. – Johnny Feb 12 '14 at 08:03
1

I'd never use C# code in Web Forms view. In addition I will avoid setting the Visible property in the markup and I will set it in the code behind on some event.

phWhatever.Visible = IsExpired;

Quite often you can avoid creating the IsExpired property.

Of course what @Johnny suggested is correct. If you need to hide what is effectively only one control you hide the control directly.

Stilgar
  • 22,354
  • 14
  • 64
  • 101