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

Export error from Grid (due to code blocks)

5 Answers 60 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jeff
Top achievements
Rank 1
Jeff asked on 08 Dec 2008, 11:53 PM

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

Sort by
0
Daniel
Telerik team
answered on 09 Dec 2008, 08:33 AM
Hello Jeff,

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.
0
Jeff
Top achievements
Rank 1
answered on 09 Dec 2008, 03:43 PM
Here is the relevent code:

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();

        }
  

0
Daniel
Telerik team
answered on 09 Dec 2008, 05:13 PM
Hello Jeff,

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.
0
Jeff
Top achievements
Rank 1
answered on 09 Dec 2008, 05:24 PM
Yes, that worked great. Thanks for the tip. When I'd tried the radcodeblock angle earlier I'd added a <script> tag which is what seems to have caused the column to no longer show up. Your method worked great.

thanks!
0
Raj Anand
Top achievements
Rank 1
answered on 19 Oct 2015, 11:46 AM

Daniel,

That worked for me also. I had same problem and you fixed it.

 Thanks man.

Tags
Grid
Asked by
Jeff
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Jeff
Top achievements
Rank 1
Raj Anand
Top achievements
Rank 1
Share this question
or