Is there a way to use “<%= someObject.ClientID %>” in an external javascript file?

前端 未结 8 1205
隐瞒了意图╮
隐瞒了意图╮ 2020-12-04 15:28

Is there a way to use \"<%= someObject.ClientID %>\" in an external javascript file?

If I use the code

<%= someObject.ClientID %>


        
相关标签:
8条回答
  • 2020-12-04 16:32

    No, it won't work like that because the javascript file is being transmitted independently as a separate file and it isn't parsed by ASP.NET on the way out, while your aspx file IS being parsed and the .NET engine is replacing the tag w/ the actual value.

    Now when your objects call those JS functions, what they could do is pass references to themselves into the function, or even pass in a dictionary list of some sort with your own key for what the control is and the assigned name.

    One thing to think about as well - IF you are going to make a JS file independent of an ASPX page, then that JS file absolutely should not presume to know anything about the specific controls of the aspx file that calls it - because later on someone else might decide, "Oh that's a good JS file, I'll incorporate it too" and it won't work for them cause their objects wont match yours. Thus the pattern you should follow would have to be more generic - thus why you'd want to pass in references or lists of identifiers.

    0 讨论(0)
  • 2020-12-04 16:33

    You could create an empty callback function in the external file:

    var MyCallback = function () { };
    

    And then in the asp/cx do something like:

    MyCallback = function () {return "<%= someObject.ClientID %>";}
    

    And then, back in your external file:

    var myClientID = MyCallback();
    
    0 讨论(0)
提交回复
热议问题