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

[Solved] Export with Ajax - loses grid layout

20 Answers 306 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Mark asked on 27 May 2009, 10:28 AM
Hi,
I have a grid with a commanditemtemplate created server-side on page_load.
Within this template is a linkbutton to export the grid contents.
The grid is ajaxified through ajaxmanager.

From the code library, i have used OnRequestStart of the ajaxmanager and a javascript function to export the grid.
This works fine, however, i am then losing all my grid settings which i set server-side within the page_load. ie; commanditemtemplate , filterbars, Itemtemplate columns, paging layout etc.
It seems to reset to the grid layout set within the aspx, but i have functions from page_load which sets all this based upon user.

Here is my javascript code...

 

function gridRequestStart(grid, eventArgs)

 

{

 

if((eventArgs.EventTarget.indexOf("exportButton") != -1))

 

{

eventArgs.EnableAjax =

false;

 

ExportGrid()

}

}

 

function ExportGrid()

 

{

 

var masterTable = $find("<%=grdPO.ClientID %>").get_masterTableView();

 

masterTable.exportToExcel();

}

Can you provide any ideas on how to use my page_load functions to set the grid layout once this script has executed ?

Many Thanks
Mark

20 Answers, 1 is accepted

Sort by
0
Mark
Top achievements
Rank 1
answered on 01 Jun 2009, 08:17 AM
Any ideas please ?
I am sure there is a simple answer, but these are usually the hardest to find !

Thanks
Mark
0
Sebastian
Telerik team
answered on 01 Jun 2009, 08:17 AM
Hello Mark,

In order to generate grid templates/template columns programmatically and keep the viewstate of the control consistent, you will need to instantiate the templates and the grid itself on OnInit as explained in these topics from the documentation:

http://www.telerik.com/help/aspnet-ajax/grdprogrammaticcreation.html
http://www.telerik.com/help/aspnet-ajax/grdcommanditemtemplate.html (the bottom section of the article)

Therefore, I suggest you alter your code accordingly and let me know whether  everything works as expected after that change.

Kind regards,
Sebastian
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.
0
Mark
Top achievements
Rank 1
answered on 01 Jun 2009, 08:34 AM
Hi,
Thanks for your reply.

I have tried your suggested solution but, still not working. Originally i created my template and grid settings within the Page_PreInit and this does not work either.

Mark
0
Sebastian
Telerik team
answered on 01 Jun 2009, 08:52 AM
Hello Mark,

Can you please post the updated code of your page (aspx and code-behind) in this forum thread using the 'Format Code Block' dialog from the upper right corner of the column editor? Thus I will examine your implementation in detail and will advice you further.

Best regards,
Sebastian
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.
0
Mark
Top achievements
Rank 1
answered on 01 Jun 2009, 08:59 AM
Hi,
Here is my grid from within aspx...
<telerik:radgrid ID="grdPO" Skin="WebBlue" GridLines="None" runat="server" PageSize="200"   
            AllowAutomaticDeletes="True" AllowPaging="True" 
            AutoGenerateColumns="False" DataSourceID="MyDataSource" 
            OnDataBound="grdPO_DataBound" OnItemDeleted="grdPO_ItemDeleted"   
            AllowSorting="True" AllowFilteringByColumn="True" ShowGroupPanel="True">  
            <PagerStyle Mode="NextPrevAndNumeric" Position="Top" /> 
            <ExportSettings ExportOnlyData="true" OpenInNewWindow="true" IgnorePaging="true" Excel-Format="ExcelML"></ExportSettings> 
            <MasterTableView Caption="Purchase Orders" AllowMultiColumnSorting="True"  EditMode="PopUp" Width="100%" CommandItemDisplay="None" DataKeyNames="PurchaseOrderID,TotalAllocated" 
                 HorizontalAlign="NotSet" AutoGenerateColumns="False">  
                <RowIndicatorColumn Visible="False"><HeaderStyle Width="20px"></HeaderStyle></RowIndicatorColumn>  
                <ExpandCollapseColumn Visible="False" Resizable="False">  
                <HeaderStyle Width="20px"></HeaderStyle> 
                </ExpandCollapseColumn> 
                <Columns> 
                    <telerik:GridButtonColumn Text="View" CommandName="View" ></telerik:GridButtonColumn> 
                                       
                    <telerik:GridButtonColumn ConfirmText="Delete this purchase order?"   
                            ConfirmDialogType="RadWindow" ConfirmTitle="Delete" ButtonType="ImageButton" 
                        CommandName="Delete" Text="Delete" UniqueName="DeleteColumn"   
                            ImageUrl="../../Images/16x16_delete_newicon.ico">  
                        <ItemStyle HorizontalAlign="Center" /> 
                    </telerik:GridButtonColumn>    
                                                           
                    <telerik:GridBoundColumn DataField="PurchaseOrderID" HeaderText="PO Ref" ReadOnly="True" SortExpression="PurchaseOrderID" 
                        UniqueName="PurchaseOrderID" > 
                    </telerik:GridBoundColumn> 
                    <telerik:GridCheckBoxColumn DataField="Complete" HeaderText="Complete" ReadOnly="True" SortExpression="Complete" 
                        UniqueName="Complete">  
                    </telerik:GridCheckBoxColumn> 
                                          
                    <telerik:GridDateTimeColumn DataField="PODate" HeaderText="Date Created" ReadOnly="True" SortExpression="PODate" 
                        UniqueName="PODate">  
                    </telerik:GridDateTimeColumn> 
                                          
                    <telerik:GridBoundColumn DataField="POLabel" HeaderText="Description" ReadOnly="True" SortExpression="POLabel" 
                        UniqueName="POLabel">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="SupplierName" HeaderText="Supplier Name" ReadOnly="True" SortExpression="SupplierName" 
                        UniqueName="SupplierNames">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="PO_Type" HeaderText="PO Type" ReadOnly="True" SortExpression="PO_Type" 
                        UniqueName="PO_Type">  
                    </telerik:GridBoundColumn>       
                    <telerik:GridBoundColumn DataField="CompanyName" HeaderText="Company" ReadOnly="True" SortExpression="CompanyName" 
                        UniqueName="CompanyName">  
                    </telerik:GridBoundColumn>      
                    <telerik:GridBoundColumn DataField="SiteName" HeaderText="Site Name" ReadOnly="True" SortExpression="SiteName" 
                        UniqueName="SiteName">  
                    </telerik:GridBoundColumn>    
                    <telerik:GridBoundColumn DataField="QuoteID" HeaderText="Quote Ref." ReadOnly="True" SortExpression="QuoteID" 
                        UniqueName="QuoteID">  
                    </telerik:GridBoundColumn>    
                    <telerik:GridNumericColumn DataField="Total_Value" HeaderText="Value Â£" ReadOnly="True" SortExpression="Total_Value" 
                        UniqueName="Total_Value">  
                    </telerik:GridNumericColumn>                                                                                                                                                                                                                                                                                                                                                             
                </Columns> 
                  
            <EditFormSettings> 
            <PopUpSettings Width="600px" Modal="true" ScrollBars="None"></PopUpSettings> 
            </EditFormSettings> 
                  
            </MasterTableView> 
            <ClientSettings AllowColumnsReorder="True" ReorderColumnsOnClient="True" 
                AllowExpandCollapse="true" AllowDragToGroup="True"</ClientSettings> 
 
            <FilterMenu Skin="WebBlue" EnableTheming="True">  
                <CollapseAnimation Type="None" Duration="0" /><ExpandAnimation Type="None" Duration="0" /> 
            </FilterMenu> 
        </telerik:radgrid> 
here is my code behind..
#Region "Grid Context Menu"  
    Private Sub GridSettings()  
        myGridSettings(mnuContext, grdPO, myBasePermissions.EXPORT_PurchaseOrders)  
        myGridCommandItemTemplate("Purchase Orders", grdPO, GridCommandItemDisplay.Top, FalseTrue, myBasePermissions.EXPORT_PurchaseOrders, True)  
    End Sub
#End Region  
 
#Region "Page" 
    
        
    
 
     
      
 
    Protected Sub Page_PreInit(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.PreInit  
        GridSettings()  
    End Sub 
 
    Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load  
  
        SetGridDataSourceInfo()  
    End Sub
#End Region 
i use GridSettings to create template and set grid settings.
Public Class MyCommandItemTemplate  
        Implements ITemplate
#Region "Declaration"  
        Protected addButton As LinkButton  
        Protected refreshButton As LinkButton  
        Protected WithEvents exportButton As LinkButton  
        Protected layoutButton As HyperLink  
        Protected addImage As Image  
        Protected refreshImage As Image  
        Protected exportImage As Image  
        Protected layoutImage As Image  
 
        Private s_SettingsID As String = "" 
        Private b_AddItem As Boolean = False 
        Private b_Refresh As Boolean = False 
        Private b_Export As Boolean = False 
        Private b_Columns As Boolean = False 
        Private s_AddItemText As String = "" 
 
        Private myGrid As RadGrid
#End Region  
 
#Region "New" 
        Public Sub New()  
            MyBase.New()  
        End Sub 
 
        Public Sub New(ByRef gGrid As RadGrid, ByVal sSettingsID As StringByVal bAddItem As BooleanByVal bRefresh As BooleanByVal bExport As BooleanByVal bColumns As BooleanByVal sAddItemText As String)  
            MyBase.New()  
            s_SettingsID = sSettingsID  
            b_AddItem = bAddItem  
            b_Refresh = bRefresh  
            b_Export = bExport  
            b_Columns = bColumns  
            If sAddItemText = "" Then 
                s_AddItemText = "Add Item" 
            Else 
                s_AddItemText = sAddItemText  
            End If 
            myGrid = gGrid  
        End Sub
#End Region  
 
#Region "InstantiateIn" 
        Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn  
            Dim s5Spaces As String = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" 
            Dim s1Space As String = "&nbsp;" 
 
            If b_AddItem Then 
                addImage = New Image  
                With addImage  
                    .ID = "addImage" 
                    .ImageUrl = "../../Images/16x16_new.ico" 
                End With 
                container.Controls.Add(addImage)  
                container.Controls.Add(New LiteralControl(s1Space))  
 
                addButton = New LinkButton  
                With addButton  
                    .ID = "addButton" 
                    .Text = s_AddItemText  
                    .CssClass = "StdHpl" 
                    .CommandName = "InitInsert" 
                End With 
                container.Controls.Add(addButton)  
                container.Controls.Add(New LiteralControl(s5Spaces))  
            End If 
            If b_Refresh Then 
                refreshImage = New Image  
                With refreshImage  
                    .ID = "refreshImage" 
                    .ImageUrl = "../../Images/16x16_refresh.ico" 
                End With 
                container.Controls.Add(refreshImage)  
                container.Controls.Add(New LiteralControl(s1Space))  
 
                refreshButton = New LinkButton  
                With refreshButton  
                    .ID = "refreshButton" 
                    .Text = "Refresh" 
                    .CssClass = "StdHpl" 
                    .CommandName = "Rebind" 
                End With 
                container.Controls.Add(refreshButton)  
                container.Controls.Add(New LiteralControl(s5Spaces))  
            End If 
            If b_Export Then 
                exportImage = New Image  
                With exportImage  
                    .ID = "exportImage" 
                    .ImageUrl = "../../Images/16x16_Excel.ico" 
                End With 
                container.Controls.Add(exportImage)  
                container.Controls.Add(New LiteralControl(s1Space))  
 
                exportButton = New LinkButton  
                With exportButton  
                    .ID = "exportButton" 
                    .Text = "Export" 
                    .CssClass = "StdHpl" 
                End With 
                container.Controls.Add(exportButton)  
                container.Controls.Add(New LiteralControl(s5Spaces))  
            End If 
            If b_Columns Then 
                layoutImage = New Image  
                With layoutImage  
                    .ID = "layoutImage" 
                    .ImageUrl = "../../Images/16x16_settings.ico" 
                End With 
                container.Controls.Add(layoutImage)  
                container.Controls.Add(New LiteralControl(s1Space))  
 
                layoutButton = New HyperLink  
                With layoutButton  
                    .ID = "layoutButton" 
                    .Text = "Grid Settings" 
                    .CssClass = "StdHpl" 
                    .Attributes.Add("OnClick""ViewGridSettings('../_GridColumnSelection/GridColumnSelection.aspx?ID=" & s_SettingsID & "')")  
                End With 
                container.Controls.Add(layoutButton)  
                container.Controls.Add(New LiteralControl(s5Spaces))  
            End If 
        End Sub
#End Region  
 
#Region "Export" 
        Private Sub exportButton_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles exportButton.Click  
            Dim myExportGrid As New TeleiosWebCRM.GridExportContents(myGrid)  
            myExportGrid = Nothing 
        End Sub
#End Region  
    End Class 
The grid seems to be losing its filter bars and the commanditemtemplate when export is clicked...

Many Thanks
Mark

0
Mark
Top achievements
Rank 1
answered on 02 Jun 2009, 08:49 AM
Hi,
The grid loses its filter row, the commanditemtemplate, and any GridButtonColumns ??!

I am at a dead-end with this issue now, as i cannot figure out why it is happening.

Any insight would be greatly appreciated...many thanks

Mark
0
Mark
Top achievements
Rank 1
answered on 02 Jun 2009, 11:53 AM
Hi,
I have now figured that i need to rebind the grid after the export...
            function gridRequestStart(grid, eventArgs)  
            {  
              if((eventArgs.EventTarget.indexOf("exportButton") != -1))  
              {  
                 eventArgs.EnableAjax = false;  
                 ExportGrid();         
              }  
            }      
            function ExportGrid()  
            {  
                var masterTable = $find("<%=grdPO.ClientID %>").get_masterTableView();  
                masterTable.exportToExcel();  
            }    
where would i place the mastertable.rebind(); ??
It does not seem to work wherever i place it ?

Thanks
Mark
0
Mark
Top achievements
Rank 1
answered on 03 Jun 2009, 08:53 AM
Hi,
Any ideas.....?

Mark
0
Sebastian
Telerik team
answered on 03 Jun 2009, 02:51 PM
Hello Mark,

As explained in my previous replies, you need to instantiate the entire grid along with the command item/columns/etc. programmatically inside OnInit and add it to the Controls collection of an asp PlaceHolder. Thus you will keep its viewstate/lifecycle intact.

Regards,
Sebastian
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.
0
Mark
Top achievements
Rank 1
answered on 03 Jun 2009, 03:06 PM

Hi Sebastian,

I have just read the help document properly !! I will give this a go now. !

 

Thanks for your help

Mark

0
Mark
Top achievements
Rank 1
answered on 03 Jun 2009, 04:24 PM
Woo Hoo !!!!!!!!!!!!!!
It works !!!!

Many Thanks Sebastian

Mark
0
Mark
Top achievements
Rank 1
answered on 03 Jun 2009, 05:39 PM
Argghhhhh !

I stand corrected, it does NOT work !

I create my grid the same as the Help example. Yet when i click Export, it removes the button columns, filter row, and commanditemtemplate !!! These are ALL created programmatically within Page_Init !!

Mark
0
Mark
Top achievements
Rank 1
answered on 04 Jun 2009, 09:10 AM
Hi Sebastian,

I have finally figured out why my grid was acting strangely !

I copied the Help document into my code and the grid "export linkbutton" within commanditemtemplate worked fine !
I then added my code for all the various grid settings etc, and it failed !!

After MUCH trial & error, i removed the export settings, and it worked !
It would appear that these settings for some reason cause the "export linkbutton" within commanditemtemplate not to work correctly.

As i have solved it, this is great, however, not a satisfactory solution.
My grid now exports everything, not just the data only.
If i add the ExportSettings so i get just the data, problem returns !!

Also, after clicking the export linkbutton, the filterbutton drop-downs no longer work ?!

Please can you advise me of any bug / solution for this ?
If it is a bug, can you provide a hotfix please as this is an urgent issue for me.

Many Thanks
Mark

 

'.ExportSettings.Excel.Format = GridExcelExportFormat.ExcelML

 

 

 

'.ExportSettings.ExportOnlyData = True

 

 

 

'.ExportSettings.IgnorePaging = True

 

 

0
Sebastian
Telerik team
answered on 04 Jun 2009, 10:56 AM
Hello Mark,

Is it possible that you ajaxified the grid and disable the ajax explicitly before the export operation takes place? If this causes the problem, you should consider the solution presented in the following KB article on our site:

http://www.telerik.com/support/kb/aspnet-ajax/grid/filtering-after-exporting-from-ajaxified-grid.aspx

If the issue seems to be different, please post your updated code in this forum post in order to examine it again and advice you further.

Kind regards,
Sebastian
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.
0
Mark
Top achievements
Rank 1
answered on 04 Jun 2009, 11:30 AM
Hi Sebastian,
Thanks for your response.

I have sorted the export issue now.
The only issue left is the filtermenu being removed after export.
I have followed the kb article and used the code...
    Protected Overloads Overrides Sub OnPreRender(ByVal e As EventArgs)  
        MyBase.OnPreRender(e)  
        Dim script As String = String.Format(" " & vbCr & vbLf & " Sys.Application.add_load(function(){{ " & vbCr & vbLf & " $find(""{0}"").setIsInPostBack = function(isInPostBack) " & vbCr & vbLf & " {{ " & vbCr & vbLf & " }}; " & vbCr & vbLf & " }}) " & vbCr & vbLf & " "Me.grdPO.FilterMenu.ClientID)  
        ScriptManager.RegisterStartupScript(Me, [GetType](), Me.ClientID & "_setIsInPostBack", script, True)  
    End Sub 
but it still does not re-instate the filtermenu.
My javascript for the export is
        function gridRequestStart(grid, eventArgs)  
        {  
          if((eventArgs.EventTarget.indexOf("exportButton") != -1))  
          {  
             eventArgs.EnableAjax = false;     
          }  
        }  
and the export is
    Protected Sub grdPO_ItemCommand(ByVal source As ObjectByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles grdPO.ItemCommand  
        if e.CommandName = "exportgrid" Then 
            grdPO.MasterTableView.ExportToExcel()  
        End If 
    End Sub 
which works great now.

Any ideas why the filtermenu is still not appearing please ?
Thanks for all your help so far Sebastian.
Mark
0
Mark
Top achievements
Rank 1
answered on 04 Jun 2009, 01:20 PM
Hi Sebastian,

I know have the filtermenu appearing after export by correcting my own code ! see below..
    Protected Sub Page_PreRender(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.PreRender  
        Dim _script As String = String.Format(" " & vbCr & vbLf & " Sys.Application.add_load(function(){{ " & vbCr & vbLf & " $find(""{0}"").setIsInPostBack = function(isInPostBack) " & vbCr & vbLf & " {{ " & vbCr & vbLf & " }}; " & vbCr & vbLf & " }}) " & vbCr & vbLf & " "Me.grdPO.FilterMenu.ClientID)  
        ScriptManager.RegisterStartupScript(Me, [GetType](), Me.ClientID & "_setIsInPostBack", _script, True)  
    End Sub 
However, this gives me a java error every postback.
Char:2
Error:'null' is null or not an object.
Code:0

Removing the above code removes the error, so i know this code is giving the error.

Can you please confirm the code above is correct ?
Many Thanks
Mark
0
Mark
Top achievements
Rank 1
answered on 05 Jun 2009, 03:39 PM
Any ideas please ?
This is the last thing i need to get to work (filtering after export)...
Other than this my grids are looking great !

Thanks
Mark
0
Accepted
Sebastian
Telerik team
answered on 09 Jun 2009, 03:00 PM
Hello Mark,

Actually the issue with the filtering menu after exporting from ajaxified grid should no longer be present in case you use the Q1 2009 release of RadControls for ASP.NET AJAX or later. Hence you will not need the custom javascript to modify the _setIsInPostBack flag explicitly.

Can you please debug your code to see whether the error originates? Additionally, I will appreciate if you provide a live url where it can be seen and troubleshoot further. Thus I will be able to assist you in addressing it.

Regards,
Sebastian
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.
0
Mark
Top achievements
Rank 1
answered on 09 Jun 2009, 03:16 PM
Hi Sebastian,
Thanks i will migrate to Q1 2009 then!

Thanks for all your help on this issue.
Mark
0
Shinu
Top achievements
Rank 2
answered on 10 Jun 2009, 04:47 AM
Hi Mark,

You may also refer the following KB article which explains how to upgrade RadControls for ASP.NET to another version or license.
Updating RadControls for ASP.NET to another version or license

Shinu
Tags
Grid
Asked by
Mark
Top achievements
Rank 1
Answers by
Mark
Top achievements
Rank 1
Sebastian
Telerik team
Shinu
Top achievements
Rank 2
Share this question
or