views:

1206

answers:

5

Can I do something like this in the markup of an asp.net page, based off the "Define DEBUG constant" setting?

#IF (DEBUG) THEN
  <asp:TextBox ID="TextBox1" runat="server">You're in debug mode</asp:TextBox>
#END IF
A: 

It would be easy enough to roll your own. You might miss some of the cooler non-compiling features of Compilation Constants but you'd definitely have the ability to add markup based on a global parameter of some sort.

Brody
A: 

How about using a Literal and then using #if DEBUG in your code-behind to inject html for your textbox into the literal? Also there are direct code blocks in ASP.NET but I don't know if they deal with #if statements; those seem to be reserved for the C# compiler.

jcollum
A: 

The close as I can get is:

<asp:Literal id="isDebug" runat="server" />
<script runat="server">
    void Page_Load()
    {
#if DEBUG
        isDebug.Text = "You're in debug mode";
#endif
    }
</script>

This would give you problems if you wanted to have anything else in your Page_Load() event; the literal code above only works if the page/control has no code behind.

If I needed to do this, I would encapuslate the above code into a user control and include that control in the pages of interest.

My test user control looks like this:

<%@ Control Language="C#" AutoEventWireup="true"  %>
<asp:Literal id="isDebug" runat="server" />
<script runat="server">    
    void Page_Load()    
    {
#if DEBUG        
        isDebug.Text = "You're in debug mode";
#endif    
    }
</script>
jrcs3
+2  A: 

This post on Phil Haack's blog covers it.

John Sheehan
Thanks for the link!
Kevin Babcock
+6  A: 
<form runat="server">
 <% #if DEBUG %>
 <asp:TextBox ID="TextBox1" runat="server">You're in debug mode</asp:TextBox>
 <% #else %>
 <asp:TextBox ID="TextBox2" runat="server">Mmm... No, I think you're not in debug mode</asp:TextBox>
 <% #endif %>
</form>

Note that you cannot assign the same ID for those text boxes.

Also note that DEBUG is true when it is set so in web.config:

<compilation debug="true">
algiecas