This is a migrated thread and some comments may be shown as answers.

RadGrid: Invalid postback encountered moving to next page on RadGrid

4 Answers 144 Views
Grid
This is a migrated thread and some comments may be shown as answers.
JR
Top achievements
Rank 1
JR asked on 20 Jul 2011, 01:53 AM

Hi Telerik,

I'm encountering the error below when i tried to use the arrow to in my radgrid to move the page to the next/previous page. The weird thing is when i move the page by clicking on the page number instead of the arrow im not encountering the error. Below is a sample code that recreates the error i'm encountering.

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.

ASPX:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication9.WebForm1" %>
  
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  
<head runat="server">
    <title></title>
</head>
<body >
    <form id="form1" runat="server">
        <telerik:RadScriptManager ID="RadScriptManager1" Runat="server" />
        <asp:Label ID="lbl_Notification" runat="server" Font-Bold="true" Font-Size="Large" Text="No Row right clicked" />
        <br />
        <telerik:RadGrid ID="RadGrid1" runat="server" 
            AllowFilteringByColumn="true"
            AutoGenerateColumns="false"
            AllowPaging="true" 
            AllowSorting="true" 
            ShowGroupPanel="true" 
            OnNeedDataSource="RadGrid1_NeedDataSource" 
            OnItemDataBound="RadGrid1_ItemDataBound"
            PageSize="15">
            <MasterTableView GroupLoadMode="Client" TableLayout="Fixed" >
                <Columns>
                    <telerik:GridBoundColumn DataField="item1" HeaderText="Item1" />
                    <telerik:GridBoundColumn DataField="item2" HeaderText="Item2" />
                    <telerik:GridBoundColumn DataField="item3" HeaderText="Item3" />
                    <telerik:GridBoundColumn DataField="item4" HeaderText="Item4" />
                    <telerik:GridBoundColumn DataField="item5" HeaderText="Item5" />
                </Columns>
            </MasterTableView>
            <ClientSettings AllowGroupExpandCollapse="True" ReorderColumnsOnClient="True" 
                AllowDragToGroup="True" AllowColumnsReorder="True" EnableRowHoverStyle="True" />            
            <FilterMenu EnableImageSprites="False"></FilterMenu>
        </telerik:RadGrid>
    </form>
</body>
</html>

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Collections.ObjectModel;
  
namespace WebApplication9
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //If i remove the binding on page load the error with regards to the post back is not encountered 
            //when clicking the arrow and the number. However the label is not properly updated and the radcontextmenu is no longer 
            //available on right click after postback.
            RadGrid1.Rebind();
        }
  
        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            ObservableCollection<GridItem> items = new ObservableCollection<GridItem>();
            for (int i = 0; i < 20; i++)
                items.Add(new GridItem(i + 1));
  
            RadGrid1.DataSource = items;
        }
  
        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.DataItem != null && e.Item.DataItem.GetType() == typeof(GridItem))
            {
                var menu = new RadContextMenu();
                menu.Targets.Add(new ContextMenuElementTarget() { ElementID = e.Item.ClientID, });
  
                RadMenuItem menu1 = new RadMenuItem("Menu1");
                menu1.Value = (e.Item.ItemIndex + 1).ToString();
                menu1.Attributes["Column1"] = (e.Item.DataItem as GridItem).item1;
                RadMenuItem menu2 = new RadMenuItem("Menu2");
                menu2.Value = (e.Item.ItemIndex + 1).ToString();
                menu2.Attributes["Column1"] = (e.Item.DataItem as GridItem).item1;
                RadMenuItem menu3 = new RadMenuItem("Menu3");
                menu3.Value = (e.Item.ItemIndex + 1).ToString();
                menu3.Attributes["Column1"] = (e.Item.DataItem as GridItem).item1;
                RadMenuItem menu4 = new RadMenuItem("Menu4");
                menu4.Value = (e.Item.ItemIndex + 1).ToString();
                menu4.Attributes["Column1"] = (e.Item.DataItem as GridItem).item1;
                RadMenuItem menu5 = new RadMenuItem("Menu5");
  
                RadMenuItem subItem1 = new RadMenuItem("subMenu1") { Value = (e.Item.ItemIndex + 1).ToString() };
                subItem1.Attributes["Column1"] = (e.Item.DataItem as GridItem).item1;
  
                RadMenuItem subItem2 = new RadMenuItem("subMenu2") { Value = (e.Item.ItemIndex + 1).ToString() };
                subItem2.Attributes["Column1"] = (e.Item.DataItem as GridItem).item1;
  
                menu5.Items.Add(subItem1);
                menu5.Items.Add(subItem2);
  
                menu.Items.Add(menu1);
                menu.Items.Add(menu2);
                menu.Items.Add(menu3);
                menu.Items.Add(menu4);
                menu.Items.Add(menu5);
                menu.ItemClick += new RadMenuEventHandler(menu_ItemClick);
                RadGrid1.Controls.Add(menu);
            }
        }
  
        void menu_ItemClick(object sender, RadMenuEventArgs e)
        {
            lbl_Notification.Text = e.Item.Text + "selected from the radcontextmenu for\nRow " + e.Item.Value + "\n with Column1= " + e.Item.Attributes["Column1"];
        }
    }
  
    public class GridItem
    {
        public string item1 { get; set; }
        public string item2 { get; set; }
        public string item3 { get; set; }
        public string item4 { get; set; }
        public string item5 { get; set; }
  
        public GridItem(int rowNum)
        {
            item1 = "dsadas" + rowNum;
            item2 = "dsadas" + rowNum;
            item3 = "itdsadsaem" + rowNum;
            item4 = "idsadsatem" + rowNum;
            item5 = "itedsadam" + rowNum;
        }
    }
}

C#

4 Answers, 1 is accepted

Sort by
0
JR
Top achievements
Rank 1
answered on 22 Jul 2011, 06:12 PM
Any ideas guys?
0
Mykhaylo
Top achievements
Rank 1
answered on 25 Jul 2011, 06:42 PM
+1 to the question. Exactly the same problem.
It looks like that issue was introduced by one of the last releases.
0
christian
Top achievements
Rank 1
answered on 30 Nov 2011, 05:30 PM
solved it,

don't rebind in your postback/pageload
this worked for me:

protected void RadGrid_OnDataSourceNeeded(object sender, EventArgs e)
        {
           DataTable table = GetTableFromCache();
           RadGridSchadePremieRapport.DataSource = table;
       //now no rebind() or databind() !
        }
0
Shinu
Top achievements
Rank 2
answered on 01 Dec 2011, 05:45 AM
Hello Mykhaylo,

When you are binding your grid with NeedDataSource event, there is no need to explicitly call Rebind()/DataBind() method.

-Shinu.
Tags
Grid
Asked by
JR
Top achievements
Rank 1
Answers by
JR
Top achievements
Rank 1
Mykhaylo
Top achievements
Rank 1
christian
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or