2

I have an ASP.NET web form where I do not want the markup processed at all based on page/compiler directives. Example:

<%@ Page ... CompilerOptions="/d:MY_FLAG" %>
<% #if MY_FLAG %> ... <% #endif %>'

I have controls in the #if block that refer to code-behind methods which are not included in the build unless a similar #define is set. The problem is that no matter how I implement conditional markup, the runtime checks for the existence of the methods within the block, even if the block is not going to be rendered. So with no method for a control that won't even exist, an exception is thrown at runtime.

I can code around this by injecting markup at runtime depending on a compiler directive in code-behind. I just want to understand why this isn't working.

To be clear, if we put #if FOO code in markup, VS will disable it and it doesn't get processed in any way in the build or run-time. I want the same functionality for markup.

That's it on my description. The following are just references to similar notes.

A note here says you can't re-use control names within conditional code. Apparently this is because of the issue above - the compiler Does process control names even if they're not going to be used.

This page and the related page by K Scott Allen (Ode to Code) describe conditional markup but that doesn't cover for the situation described here.

And this page describes conditional markup where the decision is made at run-time. I don't want the decision to be made at run-time. That's the problem - the run-time will attempt to verify that the code-behind supports the markup. In my case, if the markup doesn't 'exist' then it won't need to do that.

Community
  • 1
  • 1
TonyG
  • 1,432
  • 12
  • 31
  • I appreciate the editing volunteered by @abatishchev. However, text was removed that I intentionally included. I included "ASPX" web forms to differentiate form MVC where the solution would be different. I spelled "codebehind" and "code-behind" and included "ASPX" and other terms to ensure that search engines find this page. I also included ASP.NET in the title so that anyone looking at the SE sidebar or summaries would immediately be able to differentiate from other languages/frameworks. So thanks, but please don't be so aggressive, and my Thanks shouldn't have been removed either. – TonyG Feb 15 '13 at 21:32
  • Hi, please see [Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts) and [Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles) – abatishchev Feb 15 '13 at 23:43
  • Tag `[asp.net]` is dedicated to "ASP.NET Web Forms" and `[asp.net-mvc]` to ASP.NET MVC of any version. So they never been mixed, don't worry. – abatishchev Feb 15 '13 at 23:47
  • Proper [spelling of code-behind](http://www.google.com/search?q=code-behind) is arguable so I don't object, of course, up to you. – abatishchev Feb 16 '13 at 00:35

0 Answers0