Export to Excel in SharePoint Application Page

10 posts, 1 answers
  1. Eric Skaggs
    Eric Skaggs avatar
    39 posts
    Member since:
    Nov 2008

    Posted 08 Jan 2009 Link to this post

    I've searched through the forums and haven't found anyone with exactly this situation yet, so hopefully I can get some help from all of you smart people out there. 

    I've got an application page in a WSS 3.0 environment that has a RadGrid on it.  The grid is populated with data from a back-end SQL database and I've implemented the Export to Excel functionality as described here:  http://demos.telerik.com/aspnet-ajax/Grid/Examples/GeneralFeatures/Exporting/DefaultCS.aspx.  Exporting the data to Excel works just fine.  However, after the "Export to Excel" button has been clicked, all controls on my page are no longer functional.  I can't Export to Excel again, I can't refresh the grid, etc...everything is effectively frozen.

    To further test this, I created a standard ASP.NET Web Application (to see if it would work outside of a WSS 3.0 environment).  Using the EXACT SAME CODE, everything works just fine.  I can export, refresh the grid, and life is good. 

    My grid is ajaxified through RadAjaxManager.  The "Export to Excel" button is not ajaxified and sits in a HTML table above the grid.

    I need some guidance as to why my SharePoint environment might be giving me a problem.  To help you help me, see the below code blocks.

    This is my RadScriptManager as it sits on a master page that my SharePoint environment is using 

    1 <telerik:RadScriptManager ID="RadScriptManager1" runat="server" > 
    2 </telerik:RadScriptManager> 

    This is the code within my .aspx page for the contentplaceholder "PlaceHolderMain".  This page is deployed to SharePoint as an application page.
    1 <asp:Content ID="Main" contentplaceholderid="PlaceHolderMain" runat="server">  
    2     <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Height="75px" 
    3         Width="75px">  
    4         <img alt="Loading..." src='<%= RadAjaxLoadingPanel.GetWebResourceUrl(Page, "Telerik.Web.UI.Skins.Default.Ajax.loading.gif") %>' 
    5             style="border: 0px;" /> 
    6     </telerik:RadAjaxLoadingPanel> 
    7 <table cellpadding="5px">  
    8     <tr> 
    9         <td class="SearchLabel">  
    10             View:  
    11         </td> 
    12         <td> 
    13             <telerik:RadComboBox ID="cboViews" runat="server" Skin="WebBlue">  
    14                 <Items> 
    15                     <telerik:RadComboBoxItem Text="" Value="" /> 
    16                     <telerik:RadComboBoxItem Text="DummyData" Value="DummyData" /> 
    17                     <telerik:RadComboBoxItem Text="DummyData" Value="DummyData" /> 
    18                     <telerik:RadComboBoxItem Text="DummyData" Value="DummyData" /> 
    19                     <telerik:RadComboBoxItem Text="DummyData" Value="DummyData" /> 
    20                     <telerik:RadComboBoxItem Text="DummyData" Value="DummyData" /> 
    21                     <telerik:RadComboBoxItem Text="DummyData" Value="DummyData" /> 
    22                     <telerik:RadComboBoxItem Text="DummyData" Value="DummyData" /> 
    23                     <telerik:RadComboBoxItem Text="DummyData" Value="DummyData" /> 
    24                     <telerik:RadComboBoxItem Text="DummyData" Value="DummyData" /> 
    25                     <telerik:RadComboBoxItem Text="DummyData" Value="DummyData" /> 
    26                     <telerik:RadComboBoxItem Text="DummyData" Value="DummyData" /> 
    27                     <telerik:RadComboBoxItem Text="DummyData" Value="DummyData" /> 
    28                     <telerik:RadComboBoxItem Text="DummyData" Value="DummyData" /> 
    29                     <telerik:RadComboBoxItem Text="DummyData" Value="DummyData" /> 
    30                 </Items> 
    31             </telerik:RadComboBox> 
    32         </td> 
    33         <td> 
    34             <asp:LinkButton CssClass="SearchLabel" OnCommand="lbtnGo_Command" ID="lbtnGo" Font-Underline="true" runat="server" Text="Go"/>  
    35         </td> 
    36     </tr> 
    37 </table> 
    38 <table> 
    39     <tr> 
    40         <td align="left">  
    41             <asp:Button ID="btnExport" Width="150px" Text="Export to Excel" OnClick="btnExport_Click" runat="server" /> 
    42         </td> 
    43     </tr> 
    44 </table> 
    45 <table> 
    46     <tr> 
    47         <td> 
    48             <telerik:RadGrid   
    49                 ID="grdView"   
    50                 runat="server" 
    51                 Skin="Office2007" 
    52                 PageSize="100" 
    53                 OnNeedDataSource="grdView_NeedDataSource" 
    54                 OnExcelMLExportStylesCreated="RadGrid1_ExcelMLExportStylesCreated" 
    55                 OnExcelMLExportRowCreated="RadGrid1_ExcelMLExportRowCreated" 
    56                 > 
    57                 <MasterTableView HeaderStyle-Font-Bold="true" HeaderStyle-Wrap="false" ItemStyle-Wrap="false">  
    58                 </MasterTableView> 
    59                 <GroupingSettings CaseSensitive="false" /> 
    60             </telerik:RadGrid> 
    61         </td> 
    62     </tr> 
    63 </table> 
    64     <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">  
    65         <AjaxSettings> 
    66             <telerik:AjaxSetting AjaxControlID="lbtnGo">  
    67                 <UpdatedControls> 
    68                     <telerik:AjaxUpdatedControl ControlID="grdView" LoadingPanelID="RadAjaxLoadingPanel1" /> 
    69                 </UpdatedControls> 
    70             </telerik:AjaxSetting> 
    71             <telerik:AjaxSetting AjaxControlID="grdView">  
    72                 <UpdatedControls> 
    73                     <telerik:AjaxUpdatedControl ControlID="grdView" LoadingPanelID="RadAjaxLoadingPanel1" /> 
    74                 </UpdatedControls> 
    75             </telerik:AjaxSetting> 
    76         </AjaxSettings> 
    77     </telerik:RadAjaxManager> 
    78 </asp:Content> 

    This is my C# source
    1  public class MyClass : LayoutsPageBase     
    2  {     
    3         protected RadGrid grdView;     
    4         protected RadComboBox cboViews;     
    5         protected Button btnExport;     
    6         private string curview;     
    8         protected override void OnLoad(EventArgs e)     
    9         {     
    10                 if (!Page.IsPostBack)     
    11                 {     
    12                     grdView.Visible = false;     
    13                 }     
    14                 else     
    15                 {     
    16                     grdView.Visible = true;     
    17                 }                 
    18         }     
    20         protected void lbtnGo_Command(object sender, CommandEventArgs e)     
    21         {     
    22             grdView.Rebind();     
    23         }     
    25         protected void grdView_NeedDataSource(object o, GridNeedDataSourceEventArgs e)     
    26         {     
    27                 if (cboViews.SelectedValue != "")     
    28                 {     
    29                     curview = cboViews.SelectedValue;     
    31                     Database db = DatabaseFactory.CreateDatabase();     
    32                     DbCommand getleview = db.GetStoredProcCommand("MyStoredProcedure");     
    34                     db.AddInParameter(getleview, "View", DbType.String, curview);     
    36                     DataSet ds = db.ExecuteDataSet(getleview);     
    37                     DataTable dt = ds.Tables[0];     
    39                     grdView.DataSource = dt;     
    40                 }     
    41                 else     
    42                 {     
    43                     grdView.DataSource = string.Empty;     
    44                 }     
    45         }     
    47         protected void btnExport_Click(object sender, EventArgs e)     
    48         {     
    49             ConfigureExport();     
    50             grdView.MasterTableView.ExportToExcel();     
    51         }     
    53         public void ConfigureExport()     
    54         {     
    55             grdView.ExportSettings.ExportOnlyData = true;     
    56             grdView.ExportSettings.IgnorePaging = true;     
    57             grdView.ExportSettings.OpenInNewWindow = true;     
    58             if (cboViews.SelectedValue != "")     
    59             {     
    60                 curview = cboViews.SelectedValue;     
    61                 grdView.ExportSettings.FileName = "SomeFileName_" + curview;     
    62             }     
    63             else     
    64             {     
    65                 grdView.ExportSettings.FileName = "SomeFileName";     
    66             }     
    67         }     
    69         protected void RadGrid1_ExcelMLExportRowCreated(object source, Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLRowCreatedArgs e)     
    70         {     
    71             if (e.RowType == Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLRowType.DataRow)     
    72             {     
    73                 if (e.Row.Cells[0] != null && ((string)e.Row.Cells[0].Data.DataItem).Contains("U"))     
    74                 {     
    75                     e.Row.Cells[0].StyleValue = "MyCustomStyle";     
    76                 }     
    77             }     
    78         }     
    80         protected void RadGrid1_ExcelMLExportStylesCreated(object source, Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLStyleCreatedArgs e)     
    81         {     
    82             foreach (Telerik.Web.UI.GridExcelBuilder.StyleElement style in e.Styles)     
    83             {     
    84                 if (style.Id == "headerStyle")     
    85                 {     
    86                     style.FontStyle.Bold = true;     
    87                     style.FontStyle.Color = System.Drawing.Color.Gainsboro;     
    88                     style.InteriorStyle.Color = System.Drawing.Color.Wheat;     
    89                     style.InteriorStyle.Pattern = Telerik.Web.UI.GridExcelBuilder.InteriorPatternType.Solid;     
    90                 }     
    91                 else if (style.Id == "itemStyle")     
    92                 {     
    93                     style.InteriorStyle.Color = System.Drawing.Color.WhiteSmoke;     
    94                     style.InteriorStyle.Pattern = Telerik.Web.UI.GridExcelBuilder.InteriorPatternType.Solid;     
    95                 }     
    96                 else if (style.Id == "alternatingItemStyle")     
    97                 {     
    98                     style.InteriorStyle.Color = System.Drawing.Color.LightGray;     
    99                     style.InteriorStyle.Pattern = Telerik.Web.UI.GridExcelBuilder.InteriorPatternType.Solid;     
    100                 }     
    101             }     
    103             Telerik.Web.UI.GridExcelBuilder.StyleElement myStyle = new Telerik.Web.UI.GridExcelBuilder.StyleElement("MyCustomStyle");     
    104             myStyle.FontStyle.Bold = true;     
    105             myStyle.FontStyle.Italic = true;     
    106             myStyle.InteriorStyle.Color = System.Drawing.Color.Gray;     
    107             myStyle.InteriorStyle.Pattern = Telerik.Web.UI.GridExcelBuilder.InteriorPatternType.Solid;     
    108             e.Styles.Add(myStyle);     
    109         }   
    110  }  

    So this code works perfectly in a standard asp.net web application, but does not in my WSS 3.0 environment.  One of the obvious differences between the two is that WSS 3.0 makes use of master pages and that's where I have my RadScriptManager.  In the asp.net web application, it's on the same .aspx page.  Maybe it's because it's the end of the day and I cannot think properly any more, but I am currently at a complete loss.  I appreciate any help.

    Eric Skaggs
  2. Answer
    Rosen avatar
    3247 posts

    Posted 12 Jan 2009 Link to this post

    Hello Eric Skaggs,

    The cause for this behavior is that there is a flag (named _spFormOnSubmitCalled) in SharePoint which prevents double form submition. This flag is set when the form is submitted and clear when the response is received.
    However when exporting the response is redirected and the page is not updated, thus the flag is not cleared and page's postbacks are blocked.
    In order to workaround this behavior you should manually clear the flag when exporting. This can be achieve, for example in export button's client click function similar to the following:

    MyExportButton.OnClientClick = "_spFormOnSubmitCalled = false;"  

    Please give it a try and let us know if this helps.

    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Eric Skaggs
    Eric Skaggs avatar
    39 posts
    Member since:
    Nov 2008

    Posted 12 Jan 2009 Link to this post

    Thanks for the response.  It definitely pointed me in the right direction.  I implemented your solution by adding the OnClientClick attribute to the export button in my .aspx page instead of code-behind.  That allowed me to export multiple times, but all the other controls on the page were still not functional after the export.  I opened my master page and found the following line of code:

    1 <form runat="server" onsubmit="return _spFormOnSubmitWrapper();"

    and removed the onsubmit attribute.  This is what it looks like now:

    1 <form runat="server"

    I'm not sure if that's really going to affect anything else in my SharePoint environment, but if for some reason it does, I can have the other .aspx pages in my application use a different master page than this one is.  Thanks again for the help, I'll mark your response as the answer.
  4. Gregg
    Gregg avatar
    33 posts
    Member since:
    Sep 2007

    Posted 05 Mar 2009 Link to this post

    Is Rosen's recommendation the current Telerik-accepted solution to this problem?

    This recommendation from Rosen/Telerik Admin only keeps the export buttons alive. As Eric points out, the remaining controls on the page continue to remain non-functional.

    Eric additionally removed the onsubmit="return _spFormOnSubmitWrapper();" from his master page and implies doing so solved the problem of the other controls, but is this a practical solution? I mean, what are the complete consequences of removing that statement from the master page?

    Plus, it would be nice to see this issue addressed in the RadGrid Documentation. I don't see it covered there.




  5. Rosen
    Rosen avatar
    3247 posts

    Posted 06 Mar 2009 Link to this post

    Hi Gregg,

    Setting the _spFormOnSubmitCalled to false is actually what MS Ajax is doing when an ajax request is made thus I suspect there should be not implication using such approach. 

    About  the removing  spFormOnSubmitWrapper function call, you may instead set _spSuppressFormOnSubmitWrapper variable to true, which can be done conditionally  only where needed by outputting similar to the following script:

    function supressSubmitWraper()  
      _spSuppressFormOnSubmitWrapper = true;  

    Best wishes,
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  6. Gregg
    Gregg avatar
    33 posts
    Member since:
    Sep 2007

    Posted 06 Mar 2009 Link to this post

    Hi Rosen,

    Thanks for the quick reply. I don't fully understand the solution.

    Given: In SharePoint, script/postback functionality of a page with RadGrid w/ Export - and other controls - is killed after Export button is clicked.

    Partial solution: Set Export button's OnClientClick = "_spFormOnSubmitCalled = false;" Confirmed: This successfully brings back to life only the Export button.

    Remaining problem: All of the other controls on the page, and most notably all of the functionality of the controls inside the RadGrid, e.g. paging controls, are still dead.

    Question: Are you saying all I need to do is to add this code below to the script tag of the page to bring all of the other controls back to life? Or is it more complicated than that?

    function supressSubmitWraper()  
      _spSuppressFormOnSubmitWrapper = true;  

    It seems there is more to it b/c you say "... which can be done conditionally only where needed ..." - which seems to imply I need to insert some conditional logic that determines when/where to insert that script. Sorry that I'm misunderstanding.
  7. Rosen
    Rosen avatar
    3247 posts

    Posted 09 Mar 2009 Link to this post

    Hello Gregg,

    Indeed adding this script to the page should workaround the SharePoint's double postback check flag.

    By conditionally I mean that you can output  the script from a webpart which need it instead of removing the call from the form tag completely.

    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  8. Terrence
    Terrence avatar
    2 posts
    Member since:
    Oct 2010

    Posted 09 Nov 2010 Link to this post

    Hello All,

    Although this post is over a year old, I find myself having the sames issues as the original post. The main difference being the version of the Telerik RadGrid I am utilizing. I have an Ajaxified RadGrid built into a UserControl, I added the Export-to-Excel functionality (including the necessary scripts for the RadAjaxManager, posted here: Export from ajaxified grid). Everything works fine in my local environment, however, as with the original post, once deployed to Sharepoint the controls freeze after the first Export (Including the Export button itself). The noted difference is that rather than having a button to perform the Export, I'm using the built-in functionality with version: 2009.3.1314.35. So I'm using the RadGrid's ItemCreated event to get a reference to the Export Button. There seems to be no change in the Grids behavior utilizing this code. Any help would be greatly appreciated. Please see below:








    void radGridEvents_ItemCreated(object sender, GridItemEventArgs e)



    if (e.Item is GridCommandItem)





             GridCommandItem commandItem = (GridCommandItem)e.Item; 





              if (commandItem.NamingContainer is GridTHead

                     Button button = (Button)(commandItem.FindControl("ExportToExcelButton")); 
                    button.OnClientClick = 
    "_spFormOnSubmitCalled = false;"; //.Attributes["onclick"] 













    Thanks in advance,

    Terrence Hamm




  9. Terrence
    Terrence avatar
    2 posts
    Member since:
    Oct 2010

    Posted 09 Nov 2010 Link to this post

    Please disregard my previous post, our Sharepoint site was cached, once I did a hard refresh the attached code prevailed. Thanks!
  10. Sujay
    Sujay avatar
    1 posts
    Member since:
    Nov 2012

    Posted 09 Jan 2013 Link to this post

    //This Enables the sharepoint postback after clicking on telerik export
        if (typeof (_spBodyOnLoadFunctionNames) != 'undefined' && _spBodyOnLoadFunctionNames != null) {
        function supressSubmitWraper() {
            _spSuppressFormOnSubmitWrapper = true;
Back to Top