Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Show Div Conditionally

I have googled this and didn't find what I needed. All of the existing solutions I found say to set "visibility" to false. This will not seem to work for me as my application renders PDF which simply "hides" the DIV and leaves a big white space in it's place.

Instead, I would like to not render the HTML at all. For instance, in PHP, this could be done as simply as:

<?php if ($showDiv == true) { ?>
<div>Lorem ipsum dolor sit amet</div>
<?php } ?>

In ASP.NET MVC, I could simply pass a ViewBag variable and do the same thing.

What is the solution for this in ASP.NET (C#)?

like image 573
user1477388 Avatar asked Jan 23 '13 20:01

user1477388


4 Answers

<% if ( showDiv ) { %>
<div></div>
<% } %>

where showDiv would be a protected property in your code behind.

like image 69
Wiktor Zychla Avatar answered Oct 20 '22 21:10

Wiktor Zychla


In the aspx file

<div runat="server" id="hideableDiv">Lorem ipsum dolor sit amet</div>

And in the code behind

...
hideableDiv.Visible = false;
...
like image 22
Matt Avatar answered Oct 20 '22 23:10

Matt


use a <asp:panel> control which renders as a HTML <div>. You can then toggle the visibility. If visible is set to false, asp.net will not render any content.

    <asp:Panel id="MyPanel" runat="server">
    ...
    </asp:Panel>

   MyPanel.Visible = false;
like image 4
andleer Avatar answered Oct 20 '22 22:10

andleer


The markup

<div runat="server" id="myPdfDiv">Lorem ipsum dolor sit amet</div>

The codebehind

myPdfDiv.Visible = false;
myPdfDiv.InnerHTML = "";
like image 2
codingbiz Avatar answered Oct 20 '22 22:10

codingbiz