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

Export to Excel - 'Cols' is null or undefined

5 Answers 103 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Scott
Top achievements
Rank 1
Scott asked on 20 Jan 2012, 04:29 PM
Hi, I'm hoping someone out there can help me...

I've got a radgrid and in the  <CommandItemTemplateI have a button with CommandName="ExportToExcel"

... In the code behind, I capture the radgrid1.ItemCommand

Select Case e.CommandName
            Case "ExportToExcel"
		RadGrid1.ExportSettings.ExportOnlyData = True
RadGrid1.ExportSettings.IgnorePaging = True
RadGrid1.ExportSettings.OpenInNewWindow = True
		RadGrid1.MasterTableView.ExportToExcel()

but whenever I hit the button, I get an error within what I assume is part of the Telerik code...

Microsoft JScript runtime error: Unable to get value of the property 'Cols': object is null or undefined

    

5 Answers, 1 is accepted

Sort by
0
Richard
Top achievements
Rank 1
answered on 20 Jan 2012, 10:36 PM
Scott:

Are you enabling the built in "Excel" export button, or attempting to add one of your own?

See the Grid/Export to ExcelML live demo at http://demos.telerik.com/aspnet-ajax/grid/examples/generalfeatures/excelmlexport/defaultcs.aspx and note that you enable the built-in functionality using the <CommandItemSettings ShowExportToExcelButton="true" /> as follows:
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" GridLines="None"
    AllowPaging="true" OnExcelMLExportRowCreated="RadGrid1_ExcelMLExportRowCreated"
    OnExcelMLExportStylesCreated="RadGrid1_ExcelMLExportStylesCreated">
    <ExportSettings ExportOnlyData="true" IgnorePaging="true" OpenInNewWindow="true">
        <Excel Format="ExcelML" />
    </ExportSettings>
    <MasterTableView DataKeyNames="OrderID,ProductID" DataSourceID="SqlDataSource1" CommandItemDisplay="Top">
        <CommandItemSettings ShowExportToExcelButton="true" />
    </MasterTableView>
</telerik:RadGrid>

Then, you don't have to worry about determining which button was clicked server-side. ExportToExcel() is called under cover.

Hope this helps!
0
Scott
Top achievements
Rank 1
answered on 23 Jan 2012, 12:35 PM
Hi Jumpstart - thanks for the effort :)

I'm using my own button because I have a few custom controls that live in the command item template tag (custom search on fields that aren't displayed in the radgrid etc.) and when I use the <CommandItemSettings ShowExportToExcelButton="true" /> 
The telerik 'Export to excel' button doesn't display.
I tried getting round that problem without success, so I thought I'd use my own button. And now I'm hitting this strange 'Cols' issue.

I also tried using your example below, but the only difference I can see is this bit :
<ExportSettings ExportOnlyData="true" IgnorePaging="true" OpenInNewWindow="true">
	<Excel Format="ExcelML" />
</ExportSettings>


and this part of the radgrid tag:
OnExcelMLExportRowCreated="radgridContacts_ExcelMLExportRowCreated" OnExcelMLExportStylesCreated="radgridContacts_ExcelMLExportStylesCreated
but the above line throws me an error when I include it:
error BC30456: 'radgridContacts_ExcelMLExportRowCreated' is not a member of 'ASP.controls_ui_contacts_ascx'.
I assume it should have some code behind ?

If I completely remove my <CommandItemTemplatecolumn - the ShowExportToExcelButton appears, but I still get the same error :

Export to Excel - 'Cols' is null or undefined


Any advice greatly appreciated...
0
Scott
Top achievements
Rank 1
answered on 24 Jan 2012, 05:26 PM
Any suggestions from the Telerik admins ?  (please?!)
0
Richard
Top achievements
Rank 1
answered on 24 Jan 2012, 05:48 PM
Scott:

Here's a sample project that uses an <asp:Button ID="Button1" CssClass="button" Width="150px" Text="Export to Excel" OnClick="Button1_Click"  runat="server" /> to initiate the export to Excel.

Hope this helps!

Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Default" %>
 
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
    <title>Export RadGrid Contents to Excel</title>
    <telerik:RadStyleSheetManager ID="RadStyleSheetManager1" runat="server" />
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        <Scripts>
            <%--Needed for JavaScript IntelliSense in VS2010--%>
            <%--For VS2008 replace RadScriptManager with ScriptManager--%>
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" />
        </Scripts>
    </telerik:RadScriptManager>
    <!-- content start -->
        <div class="module" style="height: 110px; width: 92%">
            <asp:CheckBox ID="CheckBox1" Text="Export only data" runat="server"></asp:CheckBox>
            <br />
            <asp:CheckBox ID="CheckBox2" Text="Ignore paging (exports all pages)" runat="server">
            </asp:CheckBox>
            <br />
            <asp:CheckBox ID="CheckBox3" Text="Open exported data in new browser window" runat="server">
            </asp:CheckBox>
            <br />
            <br />
            <asp:Button ID="Button1" CssClass="button" Width="150px" Text="Export to Excel" OnClick="Button1_Click"
                runat="server"></asp:Button>
        </div>
 
    <br />
    <script type="text/javascript">
        function onRequestStart(sender, args) {
            if (args.get_eventTarget().indexOf("ExportToExcelButton") >= 0 ){
                args.set_enableAjax(false);
            }
        }
    </script>
 
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <ClientEvents OnRequestStart="onRequestStart" />
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
 
    <div>
        <telerik:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource1" AllowSorting="True"
            AllowPaging="True" PageSize="10" runat="server" GridLines="None" Width="95%" >
            <MasterTableView AutoGenerateColumns="False" DataKeyNames="EmployeeID" DataSourceID="SqlDataSource1"
                CommandItemDisplay="Top">
                <PagerStyle Mode="NextPrevNumericAndAdvanced" />
                 <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
                    <HeaderStyle Width="20px"></HeaderStyle>
                </RowIndicatorColumn>
                <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
                    <HeaderStyle Width="20px"></HeaderStyle>
                </ExpandCollapseColumn>
                <Columns>
                    <telerik:GridBoundColumn DataField="EmployeeID" DataType="System.Int32" FilterControlAltText="Filter EmployeeID column"
                        HeaderText="EmployeeID" ReadOnly="True" SortExpression="EmployeeID" UniqueName="EmployeeID">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="LastName" FilterControlAltText="Filter LastName column"
                        HeaderText="LastName" SortExpression="LastName" UniqueName="LastName">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="FirstName" FilterControlAltText="Filter FirstName column"
                        HeaderText="FirstName" SortExpression="FirstName" UniqueName="FirstName">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Title" FilterControlAltText="Filter Title column"
                        HeaderText="Title" SortExpression="Title" UniqueName="Title">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="HomePhone" FilterControlAltText="Filter HomePhone column"
                        HeaderText="HomePhone" SortExpression="HomePhone" UniqueName="HomePhone">
                    </telerik:GridBoundColumn>
                </Columns>
                <EditFormSettings>
                    <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                    </EditColumn>
                </EditFormSettings>
            </MasterTableView>
            <FilterMenu EnableImageSprites="False">
                <WebServiceSettings>
                    <ODataSettings InitialContainerName="">
                    </ODataSettings>
                </WebServiceSettings>
            </FilterMenu>
            <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default">
                <WebServiceSettings>
                    <ODataSettings InitialContainerName="">
                    </ODataSettings>
                </WebServiceSettings>
            </HeaderContextMenu>
        </telerik:RadGrid>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [EmployeeID], [LastName], [FirstName], [Title], [HomePhone] FROM [Employees]">
        </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

Default.aspx.cs:
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
 
public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
 
    protected void Button1_Click(object sender, EventArgs e)
    {
        ConfigureExport();
        RadGrid1.MasterTableView.ExportToExcel();
 
    }
 
    public void ConfigureExport()
    {
        RadGrid1.ExportSettings.ExportOnlyData = CheckBox1.Checked;
        RadGrid1.ExportSettings.IgnorePaging = CheckBox2.Checked;
        RadGrid1.ExportSettings.OpenInNewWindow = CheckBox3.Checked;
    }
 
}

See the form in the "radgrid_excel_export.png".
Cheers!
0
Adriano
Top achievements
Rank 1
answered on 21 Jun 2013, 09:23 AM
Had similar problem:

0x800a138f - JavaScript runtime error: Unable to get property 'Cols' of undefined or null reference

The Cause:
Accidently placed 2x RadAjaxManager, one in the masterpage or default and another one inside a contentplaceholder in the same page

Solution:
Removing one of them solved the problem



Tags
Grid
Asked by
Scott
Top achievements
Rank 1
Answers by
Richard
Top achievements
Rank 1
Scott
Top achievements
Rank 1
Adriano
Top achievements
Rank 1
Share this question
or