Escaping inline code block in Asp.Net template

喜你入骨 提交于 2020-01-02 13:30:08

问题


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";
    }


来源:https://stackoverflow.com/questions/4654747/escaping-inline-code-block-in-asp-net-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!