Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Unable to set property 'className' of undefined or null reference.

When i click the selected row in a grid i am getting the following error. It should fire gridview_selectedIndexChanged() event. but it will not fire anything. rather than it will shoe the below error.

Error : JavaScript runtime error: Unable to set property 'className' of undefined or null reference.

Error code :

ASPX Page

  var gridviewID = "<%=grdSearch.ClientID%>";         var gridview = null;

        var selectedRowIndex = -1;

        $(document).ready(function () {             gridview = $('#' + gridviewID);

        });

        function RowMouseOver(rowIndex) {             if (selectedRowIndex == rowIndex) return;

            var gridviewID = "<%=grdSearch.ClientID%>";             $('#' + gridviewID)[0].rows[rowIndex + 1].className = 'GridviewScrollItemHover';

        }

        function RowMouseOut(rowIndex) {             if (selectedRowIndex == rowIndex) return;

            var gridviewID = "<%=grdSearch.ClientID%>";             $('#' + gridviewID)[0].rows[rowIndex + 1].className = 'GridviewScrollItem';

        }

        function RowSelect(rowIndex) {             if (selectedRowIndex == rowIndex) return;

            RowReset(selectedRowIndex);

            selectedRowIndex = rowIndex;

            var gridviewID = "<%=grdSearch.ClientID%>";             $('#' + gridviewID)[0].rows[rowIndex + 1].className = 'GridviewScrollItemSelected';

        }

        function RowReset(rowIndex) {

            var gridviewID = "<%=grdSearch.ClientID%>";             $('#' + gridviewID)[0].rows[rowIndex + 1].className = 'GridviewScrollItem';

        }

ASPX.Vb

 

If e.Row.RowType = DataControlRowType.DataRow Then

               

If e.Row.RowType <> DataControlRowType.DataRow Then

                   

Return

               

End If

                e.Row.Attributes(

"onmouseover") = String.Format("RowMouseOver({0});", e.Row.RowIndex)

                e.Row.Attributes.Add(

"style", "cursor:pointer;")

                e.Row.Attributes(

"onmouseout") = String.Format("RowMouseOut({0});", e.Row.RowIndex)

                e.Row.ToolTip =

"Click to select row"

                e.Row.Attributes(

"onclick") = String.Format("RowSelect({0});", e.Row.RowIndex)

           

End If


my Requirment is :

When i selecte the particular row, it should highlight the row in diff color at the same time the index value should get display in another gridview

pls. help me to solve this problem. i am unable to find out exact solution for this.


What I have tried:

function RowReset(rowIndex) {
$('#' + gridviewID +'tbody > tr').eq(rowindex+1).addClass('GridviewScrollItem');
}

But, not working...
Posted
Updated 21-Apr-17 2:00am

1 solution

Validate the object for null value before accessing the members of the object
var row = $('#' + gridviewID)[0].rows[rowIndex + 1]
if(row1 != null){
     row1.className = 'Your Class Name'
}
 
Share this answer
 
Comments
gani7787 21-Apr-17 9:08am    
Thanks for your reply,

but, gridview_selectedIndexchanged not firing after using this code...
Karthik_Mahalingam 21-Apr-17 11:58am    
this has nothing to do with the server event. you might be missing something.
gani7787 24-Apr-17 1:27am    
Thanks.

Now i solved the rowindex error.

But, mainly i have two important points to discuss.

Scenario : when i selected the row in gridview1, then it should bring all the employee records in gridview2 based on gridview1 index selection.

Problem_1 : In gridview i have "n" number of rows. when i select then it should maintain the same position even if scrolling the rows. the below code is working for selected rows maintain the same postion. But, SeletedIndex Changed Event not Firing

e.Row.Attributes("onmouseover") = String.Format("RowMouseOver({0});", e.Row.RowIndex)

e.Row.Attributes("onmouseout") = String.Format("RowMouseOut({0});", e.Row.RowIndex)
e.Row.Attributes("onclick") = String.Format("RowSelect({0});", e.Row.RowIndex) (event not Firing)

Problem_2 : Suppose selected row not maintain the same position,but, it will bring all the records in gridview2. It means selectedIndexchanged is firing. The below code is working for that.

e.Row.Attributes("onmouseover") = String.Format("RowMouseOver({0});", e.Row.RowIndex)
e.Row.Attributes("onmouseout") = String.Format("RowMouseOut({0});", e.Row.RowIndex)
'e.Row.Attributes("onclick") = String.Format("RowSelect({0});", e.Row.RowIndex) (event not Firing)
e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(grdSearchResult, "Select$" + e.Row.RowIndex.ToString())) (event firing).

My scenario is, i want to maintain selected row in the same position also it should fire the selectedIndexchanged event (both scenario).

Below is the common script



var gridviewID = "<%=grdSearchResult.ClientID%>";
var gridview = null;

var selectedRowIndex = -1;

$(document).ready(function () {
gridview = $('#' + gridviewID);

});

function RowMouseOver(rowIndex) {
if (selectedRowIndex == rowIndex) return;

var gridviewID = "<%=grdSearchResult.ClientID%>";
$('#' + gridviewID)[0].rows[rowIndex + 1].className = 'GridviewScrollItemHover';


}

function RowMouseOut(rowIndex) {
if (selectedRowIndex == rowIndex) return;

var gridviewID = "<%=grdSearchResult.ClientID%>";
$('#' + gridviewID)[0].rows[rowIndex + 1].className = 'GridviewScrollItem';



}

function RowSelect(rowIndex) {
if (selectedRowIndex == rowIndex) return;

RowReset(selectedRowIndex);

selectedRowIndex = rowIndex;

var gridviewID = "<%=grdSearchResult.ClientID%>";
$('#' + gridviewID)[0].rows[rowIndex + 1].className = 'GridviewScrollItemSelected';

}

function RowReset(rowIndex) {
var row = $('#' + gridviewID)[0].rows[rowIndex + 1]
if (row != null) {
row.className = 'GridviewScrollItem'
}

}



where is the problem....?

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