Murray Brown
asked on
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
2. VB.net
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();
}
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
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
ASKER
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).A ttributes. Add("oncli ck", "return String.Format("cellClicked ({0},{1})" , e.Row.RowIndex, columnIndex)
e.Row.Cells(columnIndex).A
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi. Thanks for the help. I changed the line to the following and still got an error.
e.Row.Cells(columnIndex).A ttributes. Add("oncli ck", "return " & String.Format("cellClicked ({0},{1})" , e.Row.RowIndex, columnIndex))
e.Row.Cells(columnIndex).A
ASKER
Thanks. This led me in the right direction
ah cool.. Sorry if my second attempt was also mis-written. Glad you sorted it.
ASKER
the Attributes.Add was new to me so that made a big difference when I looked for answers, so thanks again
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.
Open in new window
Also ensure you have AutoEventWireup="false" in the page header.
Does this help?