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

Export to PDF|Excel|Word via CommandItemTemplate

5 Answers 253 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chase Florell
Top achievements
Rank 1
Chase Florell asked on 07 Nov 2008, 08:39 PM
I am trying to export my grid using LinkButton's in the CommandItemTemplate, but unfortunately it is not working.  I have tried setting the OnClick="LinkButton1_Click" but it does not seem to be working.

I have also tried to FindControl during the RadGrid1_ItemCreated event, but then I cannot set the OnClick property from there since it is Protected...

Any suggestions would be great
    Protected Sub LinkButton1_Click(ByVal sender As ObjectByVal e As System.EventArgs)  
        RadGrid1.ExportSettings.ExportOnlyData = True 
        RadGrid1.MasterTableView.ExportToExcel()  
    End Sub 
 
    Protected Sub LinkButton2_Click(ByVal sender As ObjectByVal e As System.EventArgs)  
        RadGrid1.MasterTableView.ExportToPdf()  
    End Sub 
 
    Protected Sub RadGrid1_ItemCreated(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemCreated  
        If (TypeOf e.Item Is GridCommandItem) Then 
            Dim cmditm As GridCommandItem = DirectCast(e.Item, GridCommandItem)  
            Dim LinkButton1 As LinkButton = DirectCast(cmditm.FindControl("LinkButton1"), LinkButton)  
            Dim LinkButton2 As LinkButton = DirectCast(cmditm.FindControl("LinkButton2"), LinkButton)  
 
            LinkButton1.OnClick = LinkButton1_Click()  
        End If 
    End Sub 

<div class="panelbody_bottom">  
    <div style="height: 20px;">&nbsp;</div> 
    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" Skin="Telerik" AllowAutomaticDeletes="false" AllowAutomaticUpdates="false" AllowAutomaticInserts="false" AllowSorting="true" AllowPaging="true">  
        <ExportSettings IgnorePaging="true" OpenInNewWindow="true" ExportOnlyData="true">  
            <Excel Format="HTML" /> 
            <Pdf AllowAdd="false" AllowCopy="true" AllowModify="true" AllowPrinting="true" Author="Anonymous" Keywords="None" PageBottomMargin="1in" PageLeftMargin="1in" PageRightMargin="1in" PageTopMargin="1in" PageTitle="My Businesses For Sale" 
                Subject="My Businesses For Sale" Title="My Businesses For Sale" PaperSize="Letter" /> 
        </ExportSettings> 
        <MasterTableView TableLayout="Auto" DataKeyNames="ID" CommandItemDisplay="Top">  
            <PagerStyle AlwaysVisible="true" Mode="NextPrevAndNumeric" /> 
            <RowIndicatorColumn> 
                <HeaderStyle Width="15px" /> 
            </RowIndicatorColumn> 
            <ExpandCollapseColumn> 
                <HeaderStyle Width="15px" /> 
            </ExpandCollapseColumn> 
            <Columns> 
                <telerik:GridBoundColumn DataField="_31" HeaderText="Broker/Agent" UniqueName="31" /> 
                <telerik:GridBoundColumn DataField="_45" HeaderText="Business Extension" UniqueName="45" /> 
                <telerik:GridBoundColumn DataField="_59" HeaderText="Description" UniqueName="59" /> 
                <telerik:GridBoundColumn DataField="_118" HeaderText="Selling Price" UniqueName="_118" DataFormatString="{0:c}" /> 
                <telerik:GridTemplateColumn DataField="DateExpires" HeaderText="Expires" UniqueName="DateExpires">  
                    <ItemTemplate> 
                        <asp:Label ID="lblDateUpdated" runat="server" Text='<%# FormatDate(DataBinder.Eval(Container.DataItem, "DateExpires"), DataBinder.Eval(Container.DataItem, "OrderID")) %>' /> 
                    </ItemTemplate> 
                </telerik:GridTemplateColumn> 
                <telerik:GridBoundColumn DataField="AdHits" HeaderText="Views" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" UniqueName="AdHits" /> 
                <telerik:GridTemplateColumn DataField="ID" HeaderText="" UniqueName="Edit">  
                    <ItemTemplate> 
                        <href='#'>edit</a> 
                    </ItemTemplate> 
                </telerik:GridTemplateColumn> 
                <telerik:GridTemplateColumn DataField="ID" HeaderText="" UniqueName="Sold">  
                    <ItemTemplate> 
                        <href='#'>sold</a> 
                    </ItemTemplate> 
                </telerik:GridTemplateColumn> 
            </Columns> 
            <CommandItemTemplate> 
                <div style="float: left; display: inline; padding-left: 5px;"><href="PostAd.aspx">post new business for sale ad</a></div>  
    <div style="float: right; display: inline; padding-right: 5px;">  
        <asp:LinkButton ID="LinkButton1" runat="server" Text="export to excel" /> 
        &nbsp;|&nbsp;  
        <asp:LinkButton ID="LinkButton2" runat="server" Text="export to pdf" /> 
    </div> 
            </CommandItemTemplate> 
        </MasterTableView> 
        <StatusBarSettings LoadingText="Loading..." /> 
    </telerik:RadGrid> 
</div> 

5 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 10 Nov 2008, 12:01 PM
Hi Chase,

Please try attaching the link buttons' click event as follows:

Protected Sub RadGrid1_ItemCreated(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemCreated     
    If (TypeOf e.Item Is GridCommandItem) Then    
        Dim cmditm As GridCommandItem = DirectCast(e.Item, GridCommandItem)     
        Dim LinkButton1 As LinkButton = DirectCast(cmditm.FindControl("LinkButton1"), LinkButton)     
        Dim LinkButton2 As LinkButton = DirectCast(cmditm.FindControl("LinkButton2"), LinkButton)     
    
        AddHandler LinkButton1.Click, AddressOf LinkButton1_Click  
        AddHandler LinkButton2.Click, AddressOf LinkButton2_Click  
    End If    
End Sub    
 

Additionally, please note that if your grid is ajaxified, you should disable the ajax through export as described here

Let me know if this helps.

Regards,
Iana
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Chase Florell
Top achievements
Rank 1
answered on 10 Nov 2008, 03:42 PM
This is great except I can't seem to get it to work for controls nested inside the grid (In the CommandItemTemplate)... only when the button is outside the grid.

I have tried running
 
    function mngRequestStarted(ajaxManager, eventArgs) {  
        if (eventArgs.EventTarget == "LinkButton1" || eventArgs.EventTarget == "LinkButton2") {  
            eventArgs.EnableAjax = false;  
        }  
    }  
 

but the ajax still fires
0
Chase Florell
Top achievements
Rank 1
answered on 10 Nov 2008, 03:45 PM
Oh, and also... When I export to Excel I use the following code
 
    Protected Sub LinkButton1_Click(ByVal sender As ObjectByVal e As System.EventArgs)  
        RadGrid1.ExportSettings.ExportOnlyData = True 
        RadGrid1.MasterTableView.ExportToExcel()  
    End Sub 
 

But the problem is that it still formats the excel.  I really just want it to populate the cells and be done, no fancy background colors or anything like that.

... Suggestions?
0
Iana Tsolova
Telerik team
answered on 11 Nov 2008, 09:36 AM
Hello Chase,

To answer your previous post:
Please modify the RadAjaxManager OnRequestStart client side event handler as follows:
function mngRequestStarted(ajaxManager, eventArgs) {     
   if (eventArgs.EventTarget.indexOf("LinkButton1") != -1 ||   
          eventArgs.EventTarget.indexOf("LinkButton2") != -1) {     
            eventArgs.EnableAjax = false;     
   }     
}   
 
Thus the exporting should work event if the export buttons are in the command item.

To answer your last post:
Check out if the Excel exporting format is Html (the default value):
RadGrid1.ExportSettings.Excel.Format = GridExcelExportFormat.Html 

Let me know how it goes.

Regards,
Iana
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Chase Florell
Top achievements
Rank 1
answered on 12 Nov 2008, 01:07 AM
I have solved this issue by moving the button outside the commanditemtemplate.

My Export however is still not exporting data only.  When I open the xls there is a white background and black gridlines.  I just want the info without the styling
    Protected Sub expExcel_Click(ByVal sender As ObjectByVal e As System.EventArgs)  
        'Export To Excel  
        RadGrid1.ExportSettings.ExportOnlyData = True 
        RadGrid1.MasterTableView.ExportToExcel()  
    End Sub 

            <ExportSettings IgnorePaging="true" OpenInNewWindow="true" ExportOnlyData="true">  
                <Excel Format="Html" /> 
            </ExportSettings> 

Thanks in advance
Tags
Grid
Asked by
Chase Florell
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Chase Florell
Top achievements
Rank 1
Share this question
or