views:

960

answers:

4

I'm trying to use a preprocessor directive in an ASPX page, but the page doesn't recognize it. Is this just something I can't do?

Background: I'm trying to include the full version of jQuery in DEBUG mode (for, well, debugging =) ), and the minified version for release. I tried this, but I'm not terribly familiar with the ASPX <% syntax. Am I just fundamentally misunderstanding what this syntax does?

<% #if DEBUG %>
<script type="text/javascript" src="resources/jquery-1.3.2.js" />
<% #else %>
<script type="text/javascript" src="resources/jquery-1.3.2.min.js" />
<% #endif %>
+1  A: 

I tried your code, and it worked fine for me. I enabled or disabled DEBUG from the system.web/compilation section in web.config, running as a web site (didn't test as a web application; might be different...).

To see what that code does, put an intentional syntax error in the page, and try to run it with debug mode enabled. The compiler will generate a link on the error page that will allow you to view the source.

Hint: the pre-processor directives are inserted into the output.

Line 218:     #if DEBUG 
Line 219:              
Line 220:              #line default
Line 221:              #line hidden
Line 222:              @__w.Write("\r\n<script type=\"text/javascript\" src=\"resources/jquery-1.3.2.js\" />\r\n");
Line 223:              
Line 224:              #line 14 "F:\Test\test.aspx"
Line 225:     #else 
Line 226:              
Line 227:              #line default
Line 228:              #line hidden
Line 229:              @__w.Write("\r\n<script type=\"text/javascript\" src=\"resources/jquery-1.3.2.min.js\" />\r\n");
Line 230:              
Line 231:              #line 16 "F:\Test\test.aspx"
Line 232:     #endif

Of course, there are other (better) ways to do what you're after...

RickNZ
And the better ways are?
Sean Gough
Interesting difference here - using #if DEBUG in the aspx page pulls from the <compilation> tag in the web.config, but when you use it in the code-behind, it pulls DEBUG from the constant from the build configuration in the project file. So they're actually accessing two different settings.
Badjer
@Sean: the OP didn't ask for alternatives, but in general it's better to keep markup in the .aspx file, and code in the code-behind, so one alternative is to do something like create a JquerySrc property that contains the conditional logic, use it to set the src property of the script tag with <%= JquerySrc %>. If the code is reused often, you could also turn it into a user control.
RickNZ
A: 

I don't think you can have preprocessor directives in the aspx unfortunately.

A simpler way to go is to simply have a property in your code-behind that feeds in the jQuery URL, then you can set preprocessor directives to declare it. Or if you'd prefer to keep the URL in the code-infront you could use a Literal control and toggle their visibility in the code-behind based on the processor directives.

For example:

code-infront:

<asp:literal id="litJQuery" EnableViewState="false" runat="Server">
<script type="text/javascript" src="resources/jquery-1.3.2.js" />
</asp:literal>
<asp:literal id="litJQueryDebug" EnableViewState="false" Visible="false" runat="Server">
<script type="text/javascript" src="resources/jquery-1.3.2.min.js" />
</asp:literal>

code-behind, in the Page_Load method:

#if DEBUG
litJQueryDebug.Visible=true;
litJQuery.Visible=false;
#endif
Tim Schneider
+2  A: 

A better approach may be to use server side code to include the script. I'd use something like

protected void Page_Load(object sender, EventArgs e)
{
#if DEBUG    
    ScriptManager.RegisterClientScriptInclude(this, this.GetType(), "JQueryScript", "resources/jquery-1.3.2.js");
    #else
    ScriptManager.RegisterClientScriptInclude(this, this.GetType(), "JQueryScript", "resources/jquery-1.3.2.min.js");
    #endif
}
Hannes Nel
+4  A: 

Interesting difference here - using #if DEBUG in the aspx page pulls from the tag in the web.config, but when you use it in the code-behind, it pulls DEBUG from the constant from the build configuration in the project file. So they're actually accessing two different settings.

Thus, as far as I can tell, this isn't actually possible.

Badjer