Invalid postback or callback argument - button in Telerik grid - asp.net

Very famous error message (see below), judging by the number of Google results. But every one of them I've seen suggests to set EnableEventValidation to false. I have searched my entire codebase, and I cannot find the string "EnableEventValidation" anywhere. Moreover, this code used to work; something I have done has obviously broken the pages. But what?
The error happens when I click on a button inside a Telerik RadGrid, declared as:
<telerik:RadGrid ID="MyGrid" Width="100%" ItemStyle-BorderColor="Gainsboro"
ItemStyle-BorderStyle="Solid" ItemStyle-BorderWidth="1px" ActiveItemStyle-BackColor="Bisque"
SelectedItemStyle-BackColor="Black" AllowPaging="True" PageSize="15" runat="server"
AllowSorting="true" OnItemCommand="MyGrid_ItemCommand" AutoGenerateColumns="false"
OnNeedDataSource="MyGrid_NeedDataSource" GridLines="Horizontal" AllowMultiRowSelection="false"
Skin="Black">
<GroupingSettings CaseSensitive="false" />
<MasterTableView Width="100%" DataKeyNames="ID" AllowFilteringByColumn="false" Font-Names="Arial"
Font-Size="10px">
<Columns>
<telerik:GridButtonColumn ButtonType="PushButton" Text="Cancel" CommandName="Cancel"
ConfirmText="Are you sure you want to cancel this?">
</telerik:GridButtonColumn>
...
</Columns>
</MasterTableView>
<PagerStyle Mode="NextPrevAndNumeric" />
<FilterMenu EnableTheming="True">
<CollapseAnimation Duration="200" Type="OutQuint" />
</FilterMenu>
</telerik:RadGrid>
click on the "Cancel" button, and here's the famous 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.

Here's the problem: in my Page_Load method I had:
protected void Page_Load(object sender, EventArgs e) {
MyGrid.Rebind();
}
The rebinding of the grid on postback was obviously screwing something up. I changed it to:
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
MyGrid.Rebind();
}
}
and everything is working now.

I had the same problem but I had no Grid.Rebind() or Grid.Databind() in my NeedDataSource method or Page_Load method. This happened just after I drag a column to be grouped and then order the grouped column ASC/DESC
I simply added
EnableEventValidation="false"
in the <%# Page %> tag of my .aspx page. The ordering fails but at least I no longer get the error. As a note everything else works perfectly except the ordering of a grouped column
here is the code I use in the NeedDataSource method
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
String connstr = ConfigurationManager.ConnectionStrings["PrimeIntegartionsConnectionString"].ConnectionString;
SqlDataSource Ds = new SqlDataSource(connstr, BuildSql()); //buildsql simply returns a SQLSelect String "select * from example"
RadGrid1.DataSource = Ds;
}

Related

OnClick Event for Button in GridView is not working

Here is my DataGridView
<asp:GridView ID="gvPredstave" runat="server" CssClass="gridview"
AlternatingRowStyle-CssClass="even" AutoGenerateColumns="True">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnRezervisi" runat="server" Text="Rezervisi" onclick="Button1_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
This is my code for click on button in GridView
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("AktivneRezervacije.aspx?korisnicko_ime=" + korisnicko_ime);
conn.Close();
}
When i click on button i got this error in browser:
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.
Does anybody know the reason why is this happening.
I tried with EnableEventValidation="false" but it doesn't work.
I recreated the error using your gridView and binding it. The answer lies on the Page_Load event. If you have it like this:
protected void Page_Load(object sender, EventArgs e)
{
bindGridView(); //code to bind the GridView
}
You will get the exception. Change it to:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindGridView();
}
}
and you should not get the error anymore.
if the page is in postback,
you have two ways to make it work
either add a postback event on your gridview
or control in on page load.
Regarding to the conn.close();
close first the connection after redirecting it to another page.
happy coding.

SelectedIndexChanged event of gridview fails to fire randomly

Hi I have a Gridview SelectedIndexChanged event. it fires on click of any row in that grid. But sometime (once in 10 tries) it doesnt fire. i have some code inside AgeGrid_SelectedIndexChanged which set the session variable. when it fails not able to get proper value.
Any Idea why it fails randomly?
i have not set EnableEventValidation="false" property for that page. And using visual studio 2005.
<Asp:GridView ID="agendaGrid" runat="server" Width="96%" GridLines="Both" EnableViewState="true" HeaderStyle-CssClass="GridViewHeader "
OnSorting = "AgeGrid_Sorting" CssClass="GridView"
AutoGenerateColumns="false" OnSelectedIndexChanged="AgeGrid_SelectedIndexChanged"
AllowPaging="true" PageSize="10" ItemTemplateType="PopUpMenu"
ShowTemplateFields="true" EnableOnClick="true" RowStyle-Font-Size="13px" RowStyle-VerticalAlign="middle"
EnablePopUpClick="true" OnRowDataBound="AgeGrid_RowDataBound" AllowSorting ="true"
>
</Asp:GridView>
Any Suggestions are welcome.
is there any thing i need to add or delete from page property or Gridview property?
protected void AgeGrid_SelectedIndexChanged(object sender, GridViewPageEventArgs e)
{
agendaGrid.PageIndex = e.NewPageIndex;
BindGrid();
}
may problem in binding data after paging, not eror in pagination.
May its helps you.

How to programmatically reach any AspxControl inside an AspXGridView's EditItemTemplate

Its very simple and i feel myself as an idiot :(
I newly started to using DevX Controls. Its documentation and sample projects are SUCKS!
My problem is:
I have an ASPxGridView on my aspx page:
<dx:ASPxGridView ID="dxdgMyGrid" runat="server" AutoGenerateColumns="False" OnStartRowEditing="DxGridStartRowEditing">
<SettingsEditing Mode="PopupEditForm" PopupEditFormHeight="200px" PopupEditFormWidth="500px"
EditFormColumnCount="2" PopupEditFormHorizontalAlign="Center" PopupEditFormVerticalAlign="Middle"
PopupEditFormModal="true" />
<Columns>
<dx:GridViewDataTextColumn FieldName="MyField1" VisibleIndex="1">
<EditFormSettings VisibleIndex="0" />
<EditItemTemplate>
<dx:ASPxDateEdit ID="dxdateMyField1" runat="server">
</dx:ASPxDateEdit>
</EditItemTemplate>
</dx:GridViewDataTextColumn>
<dx:GridViewDataColumn FieldName="MyField2" VisibleIndex="4">
<EditFormSettings VisibleIndex="1" />
<EditItemTemplate>
<dx:ASPxComboBox ID="dxcomboMyField2" runat="server">
</dx:ASPxComboBox>
</EditItemTemplate>
</dx:GridViewDataColumn>
</Columns>
How can i reach dxdateMyField1 or dxcomboMyfield2 on ASPX.CS file? I want to write:
dxcomboMyField2.DataSource = GetMyData2List();
dxcomboMyField2.SelectedItemIndex = 0;
... etc.
Thanks a lot.
You cannot access the EditItemTemplate Control Directly. You can access them at the HtmlRowCreated event as:
if (e.RowType != GridViewRowType.InlineEdit) return;
ASPxTextBox txtBox = ASPxGridView1.FindEditRowCellTemplateControl(ASPxGridView1.Columns["Name"]
as GridViewDataColumn, "ASPxTextBox1") as ASPxTextBox;
Check the documentation on Accessing Controls Contained within Templates
It is possible to cast the ASPxLabel.NamingContainer property to GridViewEditItemTemplateContainer and get a column value via the GridViewEditItemTemplateContainer.Text property.
But I like the technique of using the Init/Load event handler.When the grid switches to edit mode, the ASPxLabel.Load event is raised. Check this article The general technique of using the Init/Load event handler for implementation help.
[ASPx]
<dxe:ASPxTextBox ID="txtName" runat="server" Width="170px" OnInit="txtName_Init">
</dxe:ASPxTextBox>
[C#]
ASPxTextBox txtName;
protected void txtName_Init(object sender, EventArgs e)
{
txtName = (ASPxTextBox)sender;
GridViewEditFormTemplateContainer container = txtName.NamingContainer as GridViewEditFormTemplateContainer;
// You can remove the if statement, and try to insert a new record. You'll catch an exception, because the DataBinder returns null reference
if (!container.Grid.IsNewRowEditing)
txtName.Text = DataBinder.Eval(container.DataItem, "CategoryName").ToString();
}
Update Event:
protected void grid_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
{
e.NewValues["CategoryName"] = txtName.Text;
}
There is already an question - ASPxGridView - How to find a control inside the EditItemTemplate on DevExpress fourm .
You can use combo box init/load event handler for setting combo datasource. If that doesn't work for you, use FindEditRowCellTemplateControl (use link in comments for further explanation).

What is the purpose of EnableEventValidation and how does it work?

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" onrowediting="GridView1_RowEditing">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" />
<asp:Label ID="lblFirstColumn" runat="server" Text='<%# Eval("FirstColumn") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblEditMode" runat="server" Text="This is Edit mode"></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("FirstColumn", typeof(int));
dt.Rows.Add(100);
GridView1.DataSource = dt;
if (!IsPostBack)
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataBind();
}
If I change if (!IsPostBack) GridView1.DataBind(); to GridView1.DataBind(); and try to put GridView into Edit mode by clicking btnEdit, then I get the exception:
Invalid postback or callback argument.
Event validation is enabled using
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.
But if I set EnableEventValidation to false, then GridView1 won’t enter edit mode ( ie - GridView1_RowEditing doesn’t get called ).
BTW – same problem also occurs with other databound controls ( DataList, DetailsView )
Page. EnableEventValidation gets or
sets a value indicating whether the
page validates postback and callback
events. When the EnableEventValidation
property is set to true, ASP.NET
validates that a control event
originated from the user interface
that was rendered by that control. A
control registers its events during
rendering and then validates the
events during postback or callback
handling.
a) Why must postback and callback events be evaluated? and how exactly does evaluation happen?
b) Why would calling GridView1.Databind() on each postback cause exception when trying to put it into edit mode?
c) I also don’t see any reasons why disabling EnableEventValidation prevents GridView from entering edit mode?! Thus why did
thank you
There is a good article about how and why you would want to use EventValidation here:
http://odetocode.com/blogs/scott/archive/2006/03/21/asp-net-event-validation-and-invalid-callback-or-postback-argument-again.aspx
and MSDN:
http://msdn.microsoft.com/en-us/library/system.web.ui.page.enableeventvalidation.aspx
The short answer is event validation is designed to protect the web site from having values injected into the page that can be used to exploit your application in some way.

ASP.Net GridView UpdatePanel Paging Gives Error On Second Click

I'm trying to implement a GridView with paging inside a UpdatePanel. Everything works great when I do my first click. The paging kicks in and the next set of data is loaded quickly. However, when I then try to click a link for another page of data, I get the following error:
Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 12030
aspx code
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<contenttemplate>
<asp:GridView ID="GridView1" runat="server" CellPadding="2"
AllowPaging="true" AllowSorting="true" PageSize="20"
OnPageIndexChanging="GridView1_PageIndexChanging"
OnSorting="GridView1_PageSorting"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="ActivityLogID" HeaderText="Activity Log ID" SortExpression="ActivityLogID" />
<asp:BoundField DataField="ActivityDate" HeaderText="Activity Date" SortExpression="ActivityDate" />
<asp:BoundField DataField="ntUserID" HeaderText="NTUserID" SortExpression="ntUserID" />
<asp:BoundField DataField="ActivityStatus" HeaderText="Activity Status" SortExpression="ActivityStatus" />
</Columns>
</asp:GridView>
</contenttemplate>
</asp:UpdatePanel>
code behind
private void bindGridView(string sortExp, string sortDir)
{
SqlCommand mySqlCommand = new SqlCommand(sSQL, mySQLconnection);
SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlCommand);
mySqlAdapter.Fill(dtDataTable);
DataView myDataView = new DataView();
myDataView = dt.DefaultView;
if (sortExp != string.Empty)
{
myDataView.Sort = string.Format("{0} {1}", sortExp, sortDir);
}
GridView1.DataSource = myDataView;
GridView1.DataBind();
if (mySQLconnection.State == ConnectionState.Open)
{
mySQLconnection.Close();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bindGridView();
}
protected void GridView1_PageSorting(object sender, GridViewSortEventArgs e)
{
bindGridView(e.SortExpression, sortOrder);
}
any clues on what is causing the error on the second click?
If anything on your page that is outside UpdatePanel, is changing after the first click, or try to change, on second click is comething diferent, but your calls did get again the fist one value because there are outside UpdatePanel and did not get the update value, just get the first one again -> So product an error on second click.
Probably you have out side the UpdatePanel some data thats need to be rendered correctly.
Keep inside UpdatePanel anything that you change and use with this control.
For example the sSQL, where do you store it ? Its change ? Maybe other value changes on click ?
Probably you have out side the UpdatePanel some data thats need to be rendered correctly. Keep inside UpdatePanel anything that you change and use with this control.

Resources