Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Conditional Markup Rendering According to Web.config Key

I have a key in web.config as -

<add key="IsDemo" value ="true"/>

I want to show/hide markup based on above web.config entry for a non-server html tag without using code behind file (as there is no .cs file and there are no runat=server controls). Something similar to following pseudo code:

IF ( IsDemo == "true" )
THEN
<tr>
    <td id="tdDemoSection" colspan="2" align="left" valign="top">
        <.....>
    </td>
</tr>
ENDIF

Does anyone know that we can write such conditional logic in .aspx markup? Please help!!!

EDIT:

Section I'm hiding or showing have some data like username and password. So, I do not want user to use Firebug or Web Developer Tools to see hidden markup. markup should not go to client side.

like image 757
Parag Meshram Avatar asked Jan 19 '11 11:01

Parag Meshram


2 Answers

The syntax for something like that would be

<% if(System.Configuration.ConfigurationManager.AppSettings["IsDemo"] == "true") %>
<% { %>
<!-- Protected HTML goes here -->
<% } %>

This assumes that the page is in C#.

You can firm this code up by being more defensive around the AppSettings retrieval e.g. what happens in the case where the value is null etc.

like image 174
Tomas McGuinness Avatar answered Oct 08 '22 02:10

Tomas McGuinness


Solution:-

<% If (ConfigurationManager.AppSettings("IsDemo").ToLower().Equals("true")) Then%>
    <tr>
       <.....>
    </tr>
<% Else%>
    <tr>
        <.....>
    </tr>
<% End If%>
like image 27
Parag Meshram Avatar answered Oct 08 '22 01:10

Parag Meshram