Moving Javscript to a external js file

8 posts, 1 answers
  1. Karl
    Karl avatar
    172 posts
    Member since:
    Jun 2009

    Posted 26 Sep 2011 Link to this post

    Hi,
    I've been working on a page and doing most of the code on the clientside. To keep the page tidy I wanted to move all the code in to a .js file and work in there. When I move the code the rowselected becomes undefined. Is it possible to move the code?

    Thanks

  2. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 26 Sep 2011 Link to this post

    Karl,

    Can you show us the code you're using in this function?

    I suspect the problem is in the fact you have something like ...
    var grid = $find("<%=RadGrid1.ClientID%>");
    in your function.

    -- 
    Stuart

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Karl
    Karl avatar
    172 posts
    Member since:
    Jun 2009

    Posted 26 Sep 2011 Link to this post

    Hi,

    That's correct it does, so guess it can't be done then?

    Thanks
  5. Answer
    Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 26 Sep 2011 Link to this post

    Not so, you just need to be a bit cleverer.

    if you are simply moving your code to a .js file, you need to make sure that the values are set on the page itself, for example...
    Your .aspx file ...
    <script type="text/javascript">
      var grid = $find("<%= RadGrid1.ClientID %>");
      // any other such definitions go here
    </script>

    In your .js file you just reference this variable.

    If you are creating a single JavaScript object that has methods replacing the separate JS functions in your mark-up, you should pass the values in to the contructor for the object from within a <script> tag in your mark-up.

    -- 
    Stuart
    Don't forget to mark this thread as answered.
  6. Karl
    Karl avatar
    172 posts
    Member since:
    Jun 2009

    Posted 26 Sep 2011 Link to this post

    Thanks, will give that a shot.
  7. Karl
    Karl avatar
    172 posts
    Member since:
    Jun 2009

    Posted 26 Sep 2011 Link to this post

    Hi,

    Just tried that and I get grid is null, of course I've changed the name to the name of my grid.

    Thanks
  8. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 26 Sep 2011 Link to this post

    And is anything assigned to the grid variable?

    Just looking at some code of mine, for grids I did this ...

    var grid;
    function OnClientGridCreated(sender, e)
    {
      grid = sender;
    }

    And wired up the client-side event OnGridCreated.

    Sorry to have mislead you; it's been a while since I was in there.

    -- 
    Stuart
  9. Karl
    Karl avatar
    172 posts
    Member since:
    Jun 2009

    Posted 27 Sep 2011 Link to this post

    Hi,
    Thats the part I needed, thanks
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017