This is just a simple question: how do I exclude a piece of html on an aspx web form from being returned by the server, based on a server-side evaluation?
I'm not talking about a control specifically since they tend to have Visible parameters, but rather just some raw html.
Some people object to the following method but its one that no one has answered with and I feel that it should be shown as an option. It can be handy when used properly.
<% if (ShowLabel) {%>
<label>This will not be shown if the ShowLabel property evaluates false</label>
<%}%>
To make this work you would have a public or protected property on your page called ShowLabel which returns a boolean.
<div id="divYourDiv" runat="server">
your stuff goes here...
</div>
//Server side code...
public void ShowYourDiv(bool visible)
{
this.divYourDiv.Visible = visible;
}
Put a PlaceHolder
control around the code. It doesn't render any code for itself (like a Panel
for example), so it doesn't interfer with the html code when it's visible.
If you set the Visible
property of the PlaceHolder
to false, the code inside the PlaceHolder
will not be rendered to the page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With