Including JavaScript Conditionally in ASP.Net

In the course of building this Web site I’ve wanted several scripts I use for ads and hit counting to be disabled when I’m developing locally on my laptop. I looked for more than a few minutes and couldn’t get a definitive answer which was a bit surprising (read annoying). Some suggested using Asp:Literal, others said that code-behind was the only way. I found an easier way. Maybe this is obvious to all you old-time ASP.Net hacks but it certainly didn’t jump out at me.

The trick is to use something called a render block. You can look it up in the help for more details. There are limits to what you can do with render blocks but then anything significantly complex shouldn’t be done in a render block in the first place. In my case, I wanted to suppress a hit counter script when running locally. Here’s what I came up with.

<%@ Page Language="C#" %>  
<% if (Request.IsLocal == false)  
    { %>  
      <script src="myscript.js" type="text/javascript"></script>  
<%  } %>

The page directive goes at the top of the page. The render blocks are the portions between the <% %> delimiters.

← newer older →
.Net, Technology, Life, Whatever

Recent Posts

Quick Tip - Clear ReSharper's Cache to Fix False Errors
Quick Tip - Refined GitHub
Vertical Layout for HTML Tables
Visual Studio 2017 Installer Quick Tip
VSColorOutput for VS 2017 Released
Quick ReSharper tip for inline templates
NoSQL vs. NewSQL: What's the Difference?
More... (1077)

Donate with PayPal