34

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
Sisiutl
  • 4,915
  • 8
  • 41
  • 54

5 Answers5

62
<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
  • 2,108
  • 14
  • 13
  • is there a way to set debug automatically true when debugging and vice versa ? – Guy Apr 09 '12 at 12:12
  • 3
    @Guy `DEBUG` indicates whether or not you're using a Debug Build (As opposed to a Release build / others). I think what you want is `Debugger.IsAttached` or `HttpContext.Current.IsDebuggingEnabled` - Will be true if launched from within VS, false otherwise (unless you manually attach a debugger of course) – Basic Jun 21 '12 at 14:23
  • 1
    From my experience this works only if you have declared in the header of your ASPX or ACX file `<%@ CompilerOptions="/d:DEBUG" %>`. But then it does not automatically switch when you turn from debug to release. **Note** you can define multiple variables this way if you separate them with semicolon, e.g. `<%@ CompilerOptions="/d:myVar1; myVar2" %>`. Each of these can be checked by using a `<% #if myVar %>`statement. – Matt Sep 24 '13 at 09:09
6

If you are trying to step through javascript or prefer to minify javascript when not debugging, I prefer this approach:

<% if (Debugger.IsAttached) { %>

  <script src="jquery.js"></script>

<% } else { %>

  <script src="jquery.min.js"></script>

<% } %>

I can easily step through code when I am debugging, otherwise I want the scripts to be minified. Be sure to include the following import:

<%@ Import Namespace="System.Diagnostics" %>

Moreover, it is nice to use the Web Essentials visual studio extension to bundle/minify your javascript files so that there is only one request made to the server for your scripts.

Gaff
  • 5,507
  • 3
  • 38
  • 49
6

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,790
  • 1
  • 19
  • 35
0

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
  • 43,623
  • 55
  • 191
  • 321
-1

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
  • 2,074
  • 1
  • 18
  • 23