This is a migrated thread and some comments may be shown as answers.

Moving Javscript to a external js file

7 Answers 267 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Karl
Top achievements
Rank 1
Karl asked on 26 Sep 2011, 08:34 AM
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

7 Answers, 1 is accepted

Sort by
0
Stuart Hemming
Top achievements
Rank 2
answered on 26 Sep 2011, 08:42 AM
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

0
Karl
Top achievements
Rank 1
answered on 26 Sep 2011, 10:00 AM
Hi,

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

Thanks
0
Accepted
Stuart Hemming
Top achievements
Rank 2
answered on 26 Sep 2011, 10:18 AM
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.
0
Karl
Top achievements
Rank 1
answered on 26 Sep 2011, 01:46 PM
Thanks, will give that a shot.
0
Karl
Top achievements
Rank 1
answered on 26 Sep 2011, 01:56 PM
Hi,

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

Thanks
0
Stuart Hemming
Top achievements
Rank 2
answered on 26 Sep 2011, 02:12 PM
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
0
Karl
Top achievements
Rank 1
answered on 27 Sep 2011, 08:34 AM
Hi,
Thats the part I needed, thanks
Tags
General Discussions
Asked by
Karl
Top achievements
Rank 1
Answers by
Stuart Hemming
Top achievements
Rank 2
Karl
Top achievements
Rank 1
Share this question
or