Javascript causing an error tring to get the row and cell index when a GridView is clicked

Murray Brown
Murray Brown used Ask the Experts™
on
Hi

I am getting the error below in trying to get a GridView cell and row index using the following. I got this from the post https://forums.asp.net/t/1639326.aspx?Gridview+Cell+Click+Event+Get+Row+Column+Index
1. Javascript
// Javascript - get cell and row index
function cellClicked(row, column)
{
         document.getElementById('<% = hfAction.ClientID %>').value = "CellClick";
         document.getElementById('<% = hfRow.ClientID %>').value = row;
         document.getElementById('<% = hfColumn.ClientID %>').value = column;
         form1.submit();
}

Open in new window


2. VB.net
    Private Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound


        If e.Row.RowType = DataControlRowType.DataRow Then
            '//Following code used to change row color on mouseover
            'e.Row.Attributes.Add("onmouseover", "this.originalcolor=this.style.backgroundColor;" & " this.style.backgroundColor='#ccdfdf';")
            'e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalcolor;")
            For columnIndex As Integer = 1 To e.Row.Cells.Count - 1
                '//https://forums.asp.net/t/1639326.aspx?Gridview+Cell+Click+Event+Get+Row+Column+Index
                e.Row.Cells(columnIndex).Attributes.Add("onclick", String.Format("cellClicked({0},{1});", e.Row.RowIndex, columnIndex))
                e.Row.Cells(columnIndex).Attributes.Add("onclick", "alert(""CellClicked"");")
            Next

        End If

    End Sub

Open in new window


Server Error in '/' Application.
Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[ArgumentException: Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.]
   System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument) +9818922
   System.Web.UI.Control.ValidateEvent(String uniqueID, String eventArgument) +108
   System.Web.UI.WebControls.GridView.RaisePostBackEvent(String eventArgument) +32
   System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +15
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +9884270
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1639

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.3282.0

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Neil FlemingConsultant and developer

Commented:
Try with "return" added and no semi-colon?

Attributes.Add("onclick", "return String.Format("cellClicked({0},{1})", e.Row.RowIndex, columnIndex)

Open in new window


The second line of code looks like it is a test code line, which will just pop up a text box, and should not be there.

e.Row.Cells(columnIndex).Attributes.Add("onclick", "alert(""CellClicked"");")

Open in new window


Also ensure you have AutoEventWireup="false" in the page header.

Does this help?
Murray BrownASP.net/VSTO Developer

Author

Commented:
Hi. Thanks for the help. I changed the line to the following but it caused error ("Character constant must contain one character")
              e.Row.Cells(columnIndex).Attributes.Add("onclick", "return String.Format("cellClicked({0},{1})", e.Row.RowIndex, columnIndex)
Consultant and developer
Commented:
Sorry.. I typed it wrong: should read, I think --

add("onclick","return " & String.Format("cellClicked({0},{1})",e.Row.RowIndex,columnindex)
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Murray BrownASP.net/VSTO Developer

Author

Commented:
Hi. Thanks for the help. I changed the line to the following and still got an error.
                e.Row.Cells(columnIndex).Attributes.Add("onclick", "return " & String.Format("cellClicked({0},{1})", e.Row.RowIndex, columnIndex))
Murray BrownASP.net/VSTO Developer

Author

Commented:
Thanks. This led me in the right direction
Neil FlemingConsultant and developer

Commented:
ah cool.. Sorry if my second attempt was also mis-written. Glad you sorted it.
Murray BrownASP.net/VSTO Developer

Author

Commented:
the Attributes.Add was new to me so that made a big difference when I looked for answers, so thanks again

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial