无法从Web用户控件加载视图状态错误

时间:2015-01-01 12:57:02

标签: c# radgrid

我在Web用户控件中为Telerik RadGrid设置了一些默认属性,如排序等。然后在aspx页面上使用此用户控件.Grid完美地显示数据,但是当我点击排序时会出现以下错误。

Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.

这是我的用户控制代码。

  [ToolboxData("<{0}:CustomRadGrid runat=server> </{0}:CustomRadGrid>")]
    public class CustomRadGrid : RadGrid
    {


        RadGrid radgrid;
        public override bool AllowSorting
        {
            get
            {
                bool o = Convert.ToBoolean(ViewState["Text"]);
                return radgrid.AllowSorting != false ? true : true;
            }
            set
            {
                EnsureChildControls();
                if (value != null)
                {
                    ViewState["Text"] = value;
                }
            }
        }
        protected override void CreateChildControls()
         {
             Controls.Clear();
            radgrid = new RadGrid();
            //radgrid.AllowSorting = true;
            radgrid.AllowFilteringByColumn = true;
            radgrid.Width = Unit.Percentage(100);
            radgrid.ExportSettings.ExportOnlyData = true;
            radgrid.ExportSettings.Excel.Format = GridExcelExportFormat.ExcelML;
            this.Controls.Add(radgrid);
        }




        public CustomRadGrid()
        {
            //
            // TODO: Add constructor logic here
            //
        }
    }

在aspx中添加控件:

<customRadGrid:CustomRadGrid runat="server" ID="sa" DataSourceID="SqlDataSource1" AllowSorting="true" AutoGenerateColumns="false"> 
    <MasterTableView> 
        <Columns> 
              <telerik:GridBoundColumn DataField="EmpID" HeaderText="Employeeid ID">  
              </telerik:GridBoundColumn> <telerik:GridBoundColumn DataField="Name" HeaderText="Name"></telerik:GridBoundColumn>                
              <telerik:GridBoundColumn DataField="Salary" HeaderText="Salary"></telerik:GridBoundColumn>
        </Columns> 
    </MasterTableView> 
</customRadGrid:CustomRadGrid>

我已经尝试了一切来解决这个问题,如果有人有想法请帮忙。 提前谢谢。

0 个答案:

没有答案