RegisterRequiresControlState can only be called before and during PreRender.

Posted by user203127 on Stack Overflow See other posts from Stack Overflow or by user203127
Published on 2010-05-27T22:01:20Z Indexed on 2010/05/27 22:01 UTC
Read the original article Hit count: 1450

Filed under:

Hi When i am trying to export data from gridview to excelML I am getting error like RegisterRequiresControlState can only be called before and during PreRender. I dont know why it is happening. Please help me to sove this issue. Here is my sample code. I didnt implement any prerender method in my code. If i need to implement what should i write in that.

protected void Button4_Click(object sender, System.EventArgs e)
{
    ConfigureExport();
    RadGrid1.ExportSettings.Excel.Format = Telerik.Web.UI.GridExcelExportFormat.ExcelML;
    CheckBox1.Checked = true;
    RadGrid1.ExportSettings.ExportOnlyData = true;
    RadGrid1.MasterTableView.ExportToExcel();
}

public void ConfigureExport()
{
    RadGrid1.ExportSettings.ExportOnlyData = CheckBox1.Checked;
    RadGrid1.ExportSettings.IgnorePaging = CheckBox2.Checked;
    RadGrid1.ExportSettings.OpenInNewWindow = CheckBox3.Checked;
}

protected void RadGrid1_ExcelMLExportRowCreated(object source, Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLRowCreatedArgs e)
{
    if (e.RowType == Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLRowType.DataRow)
    {
        if (e.Row.Cells[0] != null && ((string)e.Row.Cells[0].Data.DataItem).Contains("U"))
        {
            e.Row.Cells[0].StyleValue = "MyCustomStyle";
            e.Worksheet.Name = "comcast";
        }
    }
}

protected void RadGrid1_ExcelMLExportStylesCreated(object source, Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLStyleCreatedArgs e)
{
    foreach (Telerik.Web.UI.GridExcelBuilder.StyleElement style in e.Styles)
    {
        if (style.Id == "headerStyle")
        {
            style.FontStyle.Bold = true;
            style.FontStyle.Color = System.Drawing.Color.Gainsboro;
            style.InteriorStyle.Color = System.Drawing.Color.Wheat;
            style.InteriorStyle.Pattern = Telerik.Web.UI.GridExcelBuilder.InteriorPatternType.Solid;
        }
        else if (style.Id == "itemStyle")
        {
            style.InteriorStyle.Color = System.Drawing.Color.WhiteSmoke;
            style.InteriorStyle.Pattern = Telerik.Web.UI.GridExcelBuilder.InteriorPatternType.Solid;
        }
        else if (style.Id == "alternatingItemStyle")
        {
            style.InteriorStyle.Color = System.Drawing.Color.LightGray;
            style.InteriorStyle.Pattern = Telerik.Web.UI.GridExcelBuilder.InteriorPatternType.Solid;
        }
    }

    Telerik.Web.UI.GridExcelBuilder.StyleElement myStyle = new Telerik.Web.UI.GridExcelBuilder.StyleElement("MyCustomStyle");
    myStyle.FontStyle.Bold = true;
    myStyle.FontStyle.Italic = true;
    myStyle.InteriorStyle.Color = System.Drawing.Color.Gray;
    myStyle.InteriorStyle.Pattern = Telerik.Web.UI.GridExcelBuilder.InteriorPatternType.Solid;
    e.Styles.Add(myStyle);
}

© Stack Overflow or respective owner

Related posts about c#