views:

183

answers:

5

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.

A: 

Through it/set it in a hidden div or panel (if using div, mark runat=server, both render as div). Then make the div/panel visible or keep it hidden depending on your evaluation.

Joshua
A: 

Can you wrap your html inside an asp panel, and set the visible attribute of the panel on the server-side.

Otherwise, Html is client side, you need use javascript to manipulate. You may be able to render some javascript inside your server-side code, but that normally is not a good idea.

J.W.
+3  A: 
<div id="divYourDiv" runat="server">
    your stuff goes here...
</div>

//Server side code...
public void ShowYourDiv(bool visible)
{
    this.divYourDiv.Visible = visible;
}
J.13.L
I can't seem to access divYourDiv from the codebehind. Is there something I need to do first to make this accessible?
chaiguy
make sure you set runat="server"
J.13.L
+2  A: 

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.

Guffa
+1  A: 

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.

JoshBerke
Cool I kind of like that actually. I guess some ppl don't like to mix code and markup, but I'm a coder at heart so this makes more sense to me. :)
chaiguy