Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally excluding a block of html in ASP.NET WebForms

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.

like image 954
devios1 Avatar asked Apr 04 '09 02:04

devios1


3 Answers

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.

like image 167
JoshBerke Avatar answered Oct 27 '22 01:10

JoshBerke


<div id="divYourDiv" runat="server">
    your stuff goes here...
</div>

//Server side code...
public void ShowYourDiv(bool visible)
{
    this.divYourDiv.Visible = visible;
}
like image 31
bytebender Avatar answered Oct 27 '22 03:10

bytebender


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.

like image 11
Guffa Avatar answered Oct 27 '22 01:10

Guffa