Click here to Skip to main content
13,055,408 members (52,846 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I want to make to selected gridview row highlighted. But i'm keep getting "Microsoft JScript runtime error: 'ChangeRowColor' is undefined" error once i click on my gridview.
Putting my javascript together with html page was ok. Why cannot work in external js??

Please help!

My external js

var PreviousRow;

       function ChangeRowColor(Row, Index) {
           //If last clicked row and the current clicked row are same
           if (PreviousRow == Row)
               //do nothing
           //If there is row clicked earlier
           else if (PreviousRow != null)
               //change the color of the previous row back to white
               document.getElementById(PreviousRow).style.backgroundColor = "#ffffff";
               //change the color of the current row to light gray
               document.getElementById(Row).style.backgroundColor = "lightgrey";
               //assign the current row id to the previous row id
               //for next row to be clicked
               PreviousRow = Row;


<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
            OnSorting="GridView1_Sorting" PageSize="20"
            OnRowDataBound ="GridView1_RowDataBound"
            OnRowCreated = "GridView1_RowCreated"
<script language="JavaScript" type="text/javascript" src="../Javascript.js"></script>  

My Code

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)

e.Row.Attributes.Add("onclick", "javascript:ChangeRowColor('" + rowID + "','null')");
Posted 28-Mar-13 0:07am
Hariharan Arunachalam 28-Mar-13 6:24am
Add the script statement before the grid?
BabyOreo 28-Mar-13 21:47pm
i have tried that, still getting the same error.
ryanb31 28-Mar-13 7:24am
You need to add a script tag to reference the script file.
BabyOreo 28-Mar-13 21:53pm
What do you mean? can provide example?
ryanb31 29-Mar-13 7:04am
In the head of your html add <script type="text/javascript" src="scripts/myscript.js" </script>
Tejas Vaishnav 28-Mar-13 8:42am
Please add your script tag to your header tag of page, if the page is content page of any master page then please add script tag to your master page head. then try to run your code if you still getting this error then right click on your page and view page source and find that your java script file is loaded to your page or not?
BabyOreo 28-Mar-13 9:53am
Sorry..I'm new to java script..where to add script tag?
Can you guys provide the example?
Edit: i had put the script tag in header section but not working. i can see <script type="text/javascript" src="../Javascript.js"></script> in my page source. Is it the only thing i should see to make sure it loaded?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

add your script in head tag like,

<head runat="server">
    <title> MyPage</title>
     <script type="text/javascript" src="myJavascript.js"></script>
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I put my script tag as following (in head tag) but i'm still getting "Microsoft JScript runtime error: 'ChangeRowColor' is undefined" error. please help!

<head runat="server">
       <script language="JavaScript" type="text/javascript" src="../Javascript.js"></script>
    <style type="text/css">
            height: 599px;
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Previously i put my js file in fllowing folder which does not work. Which is same folder with my solution file.
C:\...\Visual Studio 2010\Projects\ObsoleteApps
p/s: ObsoleteApps folder contains other apps folder that i want to use.

It's works if i put in following folder. Which is same folder with my specific app aspx fie.
C:\...\Visual Studio 2010\Projects\ObsoleteApps\WebApplication1

How about my other apps also need to call that js file? For eg, i have C:\...\Visual Studio 2010\Projects\ObsoleteApps\WebApplication2 that i want to call tht js file.
I need to put it into all of the folder??

Hariharan Arunachalam 29-Mar-13 0:54am
Seems so. Since the reference is using relative path, the script file needs to be part of the application to work. Is the script file included in the solution?
BabyOreo 1-Apr-13 3:34am
Yes, the script file is included in the solution. Anyway to make it centralized?
Hariharan Arunachalam 1-Apr-13 4:17am
Since the files were part of your solution, it becomes part of the virtual directory when launched. To share them, there are some methods available, the easiest of which would be creating a shared virtual directory with the files in it. Here's a couple of other methods I found on CP.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 28 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100