Escaping inline code block in Asp.Net template

2019-05-23 15:04发布

问题:

I have a page where I wish to render the following html (a small JS template)-

<script type="text/html" id="lightbox-template">
 <div id="lightbox-background"></div>
 <div id="lightbox"><%= content %><div class="bottom"></div></div>
</script>

However, the Asp.NET preprocessor is picking up on the "<%=" tag and trying to interpret it. I wish to escape this tag to allow it to be rendered, preferably from the template rather than the code behind. Is this possible?

I have managed to do this via a Literal control and setting it's text in the code behind.

回答1:

I ideally wanted to keep it within the aspx page. This is the best solution I could find (from here), which creates splits the closing > into a separate string

<script type="text/html" id="lightbox-template">
 <div id="lightbox-background"></div>
 <div id="lightbox"><%= "<%= content %" + ">" %=><div class="bottom"></div></div>  
</script>

Important bit: <%= "<%= content %" + ">" %=>



回答2:

This goes into aspx

<div> <%= GetContentString() %>  </div>

This goes into aspx.cs

 protected String GetContentString()
    {
        return "this is a content";
    }