Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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

XML
<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

C#
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
       {

e.Row.Attributes.Add("onclick", "javascript:ChangeRowColor('" + rowID + "','null')");
}
Posted
Comments
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.
ZurdoDev 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?
ZurdoDev 29-Mar-13 7:04am    
In the head of your html add <script type="text/javascript" src="scripts/myscript.js" </script>

add your script in head tag like,

XML
<head runat="server">
    <title> MyPage</title>
     <script type="text/javascript" src="myJavascript.js"></script>
</head>
 
Share this answer
 
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!

XML
<head runat="server">
    <title></title>
       <script language="JavaScript" type="text/javascript" src="../Javascript.js"></script>
    <style type="text/css">
        #form1
        {
            height: 599px;
.....
.....
 
Share this answer
 
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!
 
Share this answer
 
Comments
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.
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900