Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
Hi,
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)
 
               return;
               //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;
       }
 

My HTML
 
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
            OnSorting="GridView1_Sorting" PageSize="20"
            OnPageIndexChanging="GridView1_PageIndexChanging"
            OnRowDataBound ="GridView1_RowDataBound"
            OnRowCreated = "GridView1_RowCreated"
            AllowPaging="True">
 
        </asp:GridView>
<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 1:07am
Comments
Hariharan Arunachalam at 28-Mar-13 6:24am
   
Add the script statement before the grid?
BabyOreo at 28-Mar-13 21:47pm
   
i have tried that, still getting the same error.
ryanb31 at 28-Mar-13 7:24am
   
You need to add a script tag to reference the script file.
BabyOreo at 28-Mar-13 21:53pm
   
What do you mean? can provide example?
ryanb31 at 29-Mar-13 7:04am
   
In the head of your html add <script type="text/javascript" src="scripts/myscript.js" </script>
Tejas Vaishnav at 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 at 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
good
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>
</head>
  Permalink  
Rate this: bad
good
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">
    <title></title>
       <script language="JavaScript" type="text/javascript" src="../Javascript.js"></script>
    <style type="text/css">
        #form1
        {
            height: 599px;
.....
.....
  Permalink  
Rate this: bad
good
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??
 
Thanks!
  Permalink  
Comments
Hariharan Arunachalam at 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 at 1-Apr-13 3:34am
   
Yes, the script file is included in the solution. Anyway to make it centralized?
Hariharan Arunachalam at 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.
1. http://blogs.msdn.com/b/webdev/archive/2006/07/01/652986.aspx
2. http://weblogs.asp.net/scottgu/archive/2005/08/28/423888.aspx

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

  Print Answers RSS
0 OriginalGriff 290
1 Jochen Arndt 165
2 DamithSL 125
3 PIEBALDconsult 110
4 Garth J Lancaster 90
0 OriginalGriff 5,790
1 DamithSL 4,601
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,195


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 28 Mar 2013
Copyright © CodeProject, 1999-2014
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