I'm trying to export data to Excel from a grid but I get the following error:
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
There are two columns in the grid that require the code blocks as part of an itemtemplate. If I remove those columns the export works fine but of course I'm then missing 2 key columns.
I tried wrapping the code blocks with "radcodeblock" but that also just removed the columns from showing up within the grid. The export did work in that case but I do need the columns to show up.
Is there a way to dynamically substitute other data for those columns during export or, worst case, to ignore them in the export?
thanks for your help
5 Answers, 1 is accepted
Could you please share the problematic part of the code with us? This will help us provide a straight-to-the-point answer.
Best regards,
Daniel
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.

the aspx portion (the 2 areas causing "code block" problems with the export are in the itemTemplates):
<telerik:RadGrid ID="RadGridStudents" runat="server"
AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"
GridLines="None" OnNeedDataSource="RadGridStudents_NeedDataSource"
OnItemDataBound="RadGridStudents_ItemDataBound"
PageSize="20" Skin="Gray" Width="720px">
<AlternatingItemStyle BackColor="#ECECEC" Wrap="True" />
<PagerStyle Mode="NextPrevAndNumeric" />
<MasterTableView CommandItemDisplay="Top" GridLines="None">
<RowIndicatorColumn>
<HeaderStyle Width="20px" />
</RowIndicatorColumn>
<ExpandCollapseColumn>
<HeaderStyle Width="20px" />
</ExpandCollapseColumn>
<CommandItemTemplate>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="left" class="tablehead">
<b>Members</b> </td>
<td align="right" class="tablehead">
<asp:HyperLink ID="LinkAddMember" runat="server" >Add Member </asp:HyperLink>
|
<asp:LinkButton ID="ExcelExport" CommandArgument="RadGridStudents" OnClick="Export_to_Excel" runat="server"> Export</asp:LinkButton></td>
</tr>
</table>
</CommandItemTemplate>
<Columns>
<telerik:GridBoundColumn HeaderText="ID"
SortExpression="ID" UniqueName="ID" DataField="ID">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn HeaderText="First Name"
SortExpression="FirstName" UniqueName="FirstName" DataField="FirstName">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="LastName" HeaderText="Last Name"
SortExpression="LastName" UniqueName="LastName">
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn UniqueName="Rank" DataField="Rank" HeaderText="Rank" >
<ItemTemplate>
<asp:Image ID="RankURL" ImageUrl='<%# Convert.ToString(Eval( "RankURL" )) %>' runat="server" AlternateText='<%# Convert.ToString(Eval( "Rank" )) %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn DataField="City" HeaderText="City"
SortExpression="City" UniqueName="City">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="State" DataType="System.Int32"
HeaderText="State" SortExpression="State" UniqueName="State">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Country" DataType="System.Int32"
HeaderText="Country" SortExpression="Country" UniqueName="Country">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Role" DataType="System.Int32"
HeaderText="Role" SortExpression="Role" UniqueName="Role">
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn UniqueName="Detail" HeaderText="" HeaderStyle-HorizontalAlign="Center" itemStyle-HorizontalAlign="Center">
<ItemTemplate>
<a href="javascript:void(0)" onclick='return ShowDetail("63QJ4TGT7X",<%= Convert.ToInt32(stringLearningCenterNum) %>,<%# Convert.ToInt32(Eval( "ID" )) %>)' id="DetailLink" >
edit</a>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
<HeaderStyle Height="10px" />
<GroupHeaderItemStyle Font-Bold="True" />
<FilterMenu EnableTheming="True" Skin="Gray">
<CollapseAnimation Duration="200" Type="OutQuint" />
</FilterMenu>
</telerik:RadGrid>
The code behind portion:
protected void Export_to_Excel(object sender, EventArgs e)
{
RadGridStudents.ExportSettings.ExportOnlyData = true;
RadGridStudents.ExportSettings.IgnorePaging = true;
RadGridStudents.ExportSettings.OpenInNewWindow = true;
RadGridStudents.Page.Response.ClearHeaders();
RadGridStudents.Page.Response.Cache.SetCacheability(HttpCacheability.Private);
RadGridStudents.MasterTableView.ExportToExcel();
}
I tried the following workaround and it works as expected. Would you please try it on your side?
<ItemTemplate> |
<telerik:RadCodeBlock runat="server" ID="RadCodeBlock1"> |
<asp:Image ID="RankURL" ImageUrl='<%# Convert.ToString(Eval( "RankURL" )) %>' runat="server" |
AlternateText='<%# Convert.ToString(Eval( "Rank" )) %>' /> |
</telerik:RadCodeBlock> |
</ItemTemplate> |
Alternatively you can send your project (or simplified working version) to a formal support ticket in order to be debugged locally.
Kind regards,
Daniel
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.

thanks!

Daniel,
That worked for me also. I had same problem and you fixed it.
Thanks man.