最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Asp.Net script in external js file - Stack Overflow

programmeradmin4浏览0评论

How can I use server-side script in an external js file to grab a reference to a dom element?

The following works as intended when used as inline-script, but is returning null when I move this to an external js file.

$("#<%= gridResults.ClientID %>");

How can I use server-side script in an external js file to grab a reference to a dom element?

The following works as intended when used as inline-script, but is returning null when I move this to an external js file.

$("#<%= gridResults.ClientID %>");
Share Improve this question asked Nov 19, 2009 at 15:02 JeremyJeremy 9,32322 gold badges59 silver badges70 bronze badges 1
  • 3 See stackoverflow./questions/844970/… and stackoverflow./questions/1232465/… for a couple of general solutions to this. See stackoverflow./questions/497802/… for a jQuery solution. – Crescent Fresh Commented Nov 19, 2009 at 15:10
Add a ment  | 

4 Answers 4

Reset to default 11

You'll need to have an inline script block that creates a JavaScript variable. This block should be added before your external JavaScript file. Once you do this, you can reference that variable in your external JavaScript file.

<script type="text/javascript">
    var grid = $("#<%= gridResults.ClientID %>");
</script>

<script type="text/javascript" src="path/to/my.js"></script>

If you but a unique class on the grid using the CssClass property, you should be able to access the grid without having to know what it's clientID is.

You can't put #<%= gridResults.ClientID %>, because the gridresults.ClientID is specific to that asp page.

You could do:

<stript src="yourfile" type="text/javascript"> <!--link to external js file-->

<script type="text/javascript">
  var grid = $("#<%= gridResults.ClientID %>");

  yourfunction (grid);

</script>

What you want to happen cannot. The external javascript file is not parsed by the ASP.NET page's code behind, so the functionality of ASP.NET is not available to it.

发布评论

评论列表(0)

  1. 暂无评论