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

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

Murray BrownHead DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Neil FlemingConsultant and developerCommented:
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 BrownHead DeveloperAuthor 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)
Neil FlemingConsultant and developerCommented:
Sorry.. I typed it wrong: should read, I think --

add("onclick","return " & String.Format("cellClicked({0},{1})",e.Row.RowIndex,columnindex)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Murray BrownHead DeveloperAuthor 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 BrownHead DeveloperAuthor Commented:
Thanks. This led me in the right direction
Neil FlemingConsultant and developerCommented:
ah cool.. Sorry if my second attempt was also mis-written. Glad you sorted it.
Murray BrownHead DeveloperAuthor Commented:
the Attributes.Add was new to me so that made a big difference when I looked for answers, so thanks again
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.