简体   繁体   中英

RegisterClientScriptBlock CDATA inline declarations

I'm moving significant amounts of header includes into codebehind when migrating from aspx pages into ascx controls, I've been wrapping them in RegisterClientScriptBlocks and using linq to keep the large multiline tidy.

However have noticed that the inline declarations <%serverside.code%> are now not getting executed.

ClientScript.RegisterClientScriptBlock(GetType(Page), "test", <a><![CDATA[
        <script type="text/javascript">
            testValue = '<%=Page.Title%>';
        </script>]]></a>, True)

Produces;

<script type="text/javascript">
  testValue = '<%=Page.Title%>';
</script>

To solve this I ended the cdata block with a .Value and appended the code variable, then started a new cdata block with the rest of the multiline statement

ClientScript.RegisterClientScriptBlock(GetType(Page), "test", <![CDATA[
    <script type="text/javascript">
        testValue = ']]>.Value + Page.Title + <![CDATA[';
        //more code
    </script>
  ]]>.Value, True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM