11

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.

Parag Meshram
  • 8,281
  • 10
  • 52
  • 88

3 Answers3

24

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.

UweB
  • 4,080
  • 2
  • 16
  • 28
Tomas McGuinness
  • 7,651
  • 3
  • 28
  • 40
6

Solution:-

<% If (ConfigurationManager.AppSettings("IsDemo").ToLower().Equals("true")) Then%>
    <tr>
       <.....>
    </tr>
<% Else%>
    <tr>
        <.....>
    </tr>
<% End If%>
Parag Meshram
  • 8,281
  • 10
  • 52
  • 88
2

If I understand it right, you don't want to use server-side (aspx components, with runat="server" attribute) and just want to control display of html on aspx page then try this solution.

Create a property in codebehind file (or better still in some other config helper class):

//IN C# (OR VB) file
protected string Demo{
    get{ 
            return ConfigurationManager.AppSettings["IsDemo"]=="true"?
                   "none":"block";
      }
}

In aspx page:

<tr style="display:<%= Demo%>;">
    <td>blah blah</td>
</tr>
TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188
  • 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. – Parag Meshram Jan 19 '11 at 11:54
  • 1
    Why the hell you are putting user-names and password on page? – TheVillageIdiot Jan 19 '11 at 11:56
  • Yeah. Your question is valid. :) I had asked same question to my client. But actually, it is not a password, but it is an Access code which is shared among several users. – Parag Meshram Jan 19 '11 at 11:58
  • then there is no simple way other than using server side control so that the stuff is never rendered in the first place. – TheVillageIdiot Jan 19 '11 at 12:01