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

RadGrid User Control Issue

5 Answers 360 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mahesh Babu
Top achievements
Rank 1
Mahesh Babu asked on 10 Jul 2009, 09:01 AM
Hi,

I am trying to create user control which has radgrid and sqldatasource controls. I added radgrid to a a new ascx as given below:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs"
    Inherits="Controls_WebUserControl"  %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>

<telerik:RadGrid AutoGenerateColumns="False" ID="RadGrid1" DataSourceID="SqlDS" AllowFilteringByColumn="true"
    AllowPaging="True" AllowSorting="True" runat="server"  OnPageIndexChanged="RadGrid1_PageIndexChanged" Skin="Office2007" PageSize="10" EnableViewState="true">
    <PagerStyle Mode="NextPrevNumericAndAdvanced" />
    <MasterTableView>
        <PagerStyle Mode="NextPrevNumericAndAdvanced" />
        <Columns>
            <telerik:GridBoundColumn DataField="RollNo" SortExpression="RollNo" HeaderButtonType="TextButton" ShowSortIcon="true"
                AutoPostBackOnFilter="false">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="StudentName" SortExpression="StudentName" HeaderButtonType="TextButton"
                ShowSortIcon="true">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Address" SortExpression="Address" HeaderButtonType="TextButton" ShowSortIcon="true">
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
<asp:SqlDataSource ID="SqlDS" ConnectionString="<%$ ConnectionStrings:cemiConnectionString %>"
    ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM Student" runat="server" />


When I am changing pages using 1,2,3, it is working fine. But, when I click on pager images like next,previous,first, last is generating 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.




If the move the raggrid and sqldatasource to a new web page(default.aspx), everything is working fine.
Can I know the issue why it is generating error when I am adding it to user control???

Thanks,
Mahesh

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 10 Jul 2009, 10:12 AM
Hello Mahesh,

Check if you are calling DataBind() or Rebind() method for the grid in the PageLoad event. If so, try adding these methods within some condition check based on your scenario. If not, try setting the EnableEventValidation attribute in the page Directive of your aspx page where the user control is added to False as shown below:
aspx:
<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeFile="Default.aspx.cs" Inherits="_Default" %>  

Thanks
Princy.
0
Luis
Top achievements
Rank 1
answered on 25 Sep 2009, 02:04 PM
hi,
i solved the problem as i read in one post in the forum

binding the grid in the oninit(C#), init(VB) event

Protected Sub RadGrid1_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGrid1.Init
    RadGrid1.DataBind()
End Sub


thanks to "Hans Notelteirs"
0
Rohan
Top achievements
Rank 1
answered on 01 Nov 2012, 03:11 PM
Hi all,
i want to create User control using rad grid ,the rad grid is generated dynamically ...... please provide any link or document or example.
Thank You .
0
Shinu
Top achievements
Rank 2
answered on 02 Nov 2012, 06:27 AM
Hi,

I'm not quite sure about your requirement. Please take a look into the following code snippet I tried to create a UserControl using Programmatic RadGrid.

page.ascx
<asp:PlaceHolder ID="pl" runat="server"></asp:PlaceHolder>
<asp:SqlDataSource ID="SqlDataSource1" ConnectionString="<%$ConnectionStrings:NorthwindConnectionString3%>" runat="server" SelectCommand="select top 5 * from Employees" ></asp:SqlDataSource>

page.ascx.cs:
protected void Page_Load(object sender, EventArgs e)
{
     RadGrid grid = new RadGrid();
     grid.ID = "RadGrid1";
     grid.AutoGenerateEditColumn = true;
     grid.DataSourceID = "SqlDataSource1";
     grid.AutoGenerateColumns = false;
     grid.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;
     grid.MasterTableView.AllowMultiColumnSorting = true;
     GridBoundColumn boundColumn = new GridBoundColumn();
     boundColumn.DataField = "EmployeeID";
     boundColumn.HeaderText = "EmployeeID ID";
     grid.MasterTableView.Columns.Add(boundColumn);
     this.pl.Controls.Add(grid);
}

Please check this Help Documentation to know about loading User Control with RadGrid at Runtime.

Thanks,
Shinu.
0
Rafael
Top achievements
Rank 1
answered on 12 Dec 2012, 01:16 PM
Is It.. tks.. C#

protected void RadGrid1_Init(object sender, EventArgs e)
{
RadGrid1.DataBind();
}
    
Tags
Grid
Asked by
Mahesh Babu
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Luis
Top achievements
Rank 1
Rohan
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Rafael
Top achievements
Rank 1
Share this question
or