protected void AddToCartButton_Click(object sender, EventArgs e)
{
ShoppingCart cart = ShoppingCart.GetShoppingCart();
cart.AddItem(((Button)sender).CommandArgument);
// Redirect the user to view their shopping cart
Response.Redirect("ViewCart.aspx");
}
when I debug, if I click the button, the event does not fire and I am getting the following error:
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) +8776118
System.Web.UI.Control.ValidateEvent(String uniqueID, String eventArgument) +113
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +35
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272
EnableEventValidation was not declared. I set it to false a syou mentioned and it worked. But this is a security risk and I am not sure why it started giving this error. By the way, I have a [WebMethod] also defined in the code behind. Does it make an impact
on this issue? I have not seen it anywhere else.
The definition of this method as a security risk is overly exaggerated, IMHO. I've still to come across a valid simulation of this setting being used by malicious users.
Theguzu
Member
77 Points
139 Posts
Why an Invalid Postback
Feb 03, 2012 12:22 AM|LINK
Can anybody help me solve this issue. I have apage with a gridview where I have a button in rows tied to the back end code as:
<asp:TemplateField> <ItemTemplate> <asp:Button ID="AddToCartButton" runat="server" Text="Add To Cart" CommandArgument='<%# Eval("NDC") %>' OnClick="AddToCartButton_Click" /> </ItemTemplate> </asp:TemplateField>and code behind as:
protected void AddToCartButton_Click(object sender, EventArgs e) { ShoppingCart cart = ShoppingCart.GetShoppingCart(); cart.AddItem(((Button)sender).CommandArgument); // Redirect the user to view their shopping cart Response.Redirect("ViewCart.aspx"); }when I debug, if I click the button, the event does not fire and I am getting the following error:
OnoSendai
Participant
1144 Points
239 Posts
Re: Why an Invalid Postback
Feb 03, 2012 05:58 PM|LINK
Just as a hint - try adding (or changing) the following line to the page directive (the first line on the aspx page):
And let us know if it works for you.
Theguzu
Member
77 Points
139 Posts
Re: Why an Invalid Postback
Feb 03, 2012 06:29 PM|LINK
EnableEventValidation was not declared. I set it to false a syou mentioned and it worked. But this is a security risk and I am not sure why it started giving this error. By the way, I have a [WebMethod] also defined in the code behind. Does it make an impact on this issue? I have not seen it anywhere else.
OnoSendai
Participant
1144 Points
239 Posts
Re: Why an Invalid Postback
Feb 03, 2012 07:14 PM|LINK
The definition of this method as a security risk is overly exaggerated, IMHO. I've still to come across a valid simulation of this setting being used by malicious users.
roopeshreddy
All-Star
20155 Points
3328 Posts
Re: Why an Invalid Postback
Feb 04, 2012 04:17 AM|LINK
Hi,
May be you are using ASP.NET AJAX UpdatePanel's for the GridView!
If yes, then try registering the control to the ScriptManager!
http://stackoverflow.com/questions/1278433/how-to-register-click-event-of-button-in-gridview-header-with-ajax-updatepanel
Hope it helps u...
Roopesh Reddy C
Roopesh's Space