Is it possible to interpolate my angle bracket, percent, equals <%= %> syntax in external javascript files?

后端 未结 4 1397
一生所求
一生所求 2021-02-09 23:31

Often times when mixing jQuery with asp.net I need to use asp .net angle bracket percent, <% %>, syntax within a jQuery selector.

If I would like to separate the Jav

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-09 23:55

    If you want to evaluate <% code blocks %> as ASP.NET code in a JavaScript file, you can just put the JavaScript in an ASPX file and reference it from a SCRIPT element.

    script.js.aspx:

    function hideElements()
     { <% foreach(var elementId in Request.QueryString["hide"].Split(',') { %>
       jQuery('#' + <%= elementId %>).hide('slow');
       <% } %>
     }
    

    page.aspx:

    
    

    page.aspx.cs:

    public string GetElementIds() 
     {
       return string.Join(",", new []{control1.ClientID, control2.ClientID});
     }
    

提交回复
热议问题