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

ExportToCSV from HeadreContextMenu

2 Answers 85 Views
Grid
This is a migrated thread and some comments may be shown as answers.
mabs
Top achievements
Rank 1
mabs asked on 22 Sep 2010, 10:44 AM
I have been able to export to csv by adding a button to the CommandItemTemplate, then in server side findong the control and setting registerPostBackControl,
    Protected Sub RadGrid1_ItemCreated1(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemCreated
 
 ....
 
                Dim btn_excel As LinkButton = TryCast(item.FindControl("btn_excel"), LinkButton)
 
                ScriptManager.GetCurrent(Page).RegisterPostBackControl(btn_excel)
 
 ....
 
End Sub

When fired this calls the export and fires correctly prompting the user to view or download.

However I have changed this to now appear as an item in the headerContextMenu, when the user clicks on the export option, the same code fires in the same order, however instead of exporting it just rebinds the data to the grid, the function i use is below,

Sub buildExport(ByVal type As String)
       RadGrid1.MasterTableView.Columns.Clear()
 
       Dim boundColumn As GridBoundColumn
 
       For Each column As DataColumn In Datasource.Columns
 
           If GridDataKeyNames <> "" Then
               If Not GridDataKeyNames.Contains(column.ColumnName.ToString()) Then
                   If column.ColumnName.ToString().ToLower().IndexOf("_hidden") < 0 And column.ColumnName.ToString().ToLower().IndexOf("rowclick") < 0 Then
 
                       boundColumn = New GridBoundColumn
                       RadGrid1.MasterTableView.Columns.Add(boundColumn)
 
                       boundColumn.DataField = column.ColumnName
                       boundColumn.HeaderText = column.ColumnName.Replace("_", " ")
                   Else
                       boundColumn = New GridBoundColumn
                       RadGrid1.MasterTableView.Columns.Add(boundColumn)
 
                       boundColumn.DataField = column.ColumnName
                       boundColumn.HeaderText = column.ColumnName
 
                       boundColumn.Visible = False
                   End If
 
               Else
 
               End If
           Else
               If column.ColumnName.ToString().ToLower().IndexOf("_hidden") < 0 And column.ColumnName.ToString().ToLower().IndexOf("rowclick") < 0 Then
 
                   boundColumn = New GridBoundColumn
                   RadGrid1.MasterTableView.Columns.Add(boundColumn)
 
                   boundColumn.DataField = column.ColumnName
                   boundColumn.HeaderText = column.ColumnName.Replace("_", " ").ToLower()
               Else
                   boundColumn = New GridBoundColumn
                   RadGrid1.MasterTableView.Columns.Add(boundColumn)
 
                   boundColumn.DataField = column.ColumnName
                   boundColumn.HeaderText = column.ColumnName.ToLower()
 
                   boundColumn.Visible = False
               End If
           End If
 
 
       Next
 
       RadGrid1.DataSource = Datasource
       RadGrid1.DataBind()
 
       RadGrid1.PageSize = Datasource.Rows.Count()
       RadGrid1.ExportSettings.IgnorePaging = True
 
       Select Case type
           Case "excel"
               RadGrid1.ExportSettings.ExportOnlyData = True
               RadGrid1.MasterTableView.ExportToCSV()
           Case "pdf"
               RadGrid1.ExportSettings.OpenInNewWindow = True
               RadGrid1.MasterTableView.ExportToPdf()
       End Select
 
   End Sub

Like i said boths methods in exporting call the same function, but only the contextItem will work.  I am databinding the grid again as the user may have hidden some columns, so this way all coulmns are provided, also needed to remove the boundColumn.DataFormatString = "<nobr>{0:N}</nobr>"
which is created when the grid is loaded to prevent word wrap.

I create the headerMenuItems in the following way

Private Sub addMenuItem(ByVal sender As Object, ByVal e As System.EventArgs)
 
    Dim menu As RadContextMenu = RadGrid1.HeaderContextMenu
    Dim item As New RadMenuItem
    item.Text = "Save Layout"
    item.Value = "save"
    item.Attributes("ColumnName") = String.Empty
    item.Attributes("TableID") = String.Empty
    menu.Items.Add(item)
    item = New RadMenuItem
    item.Attributes("ColumnName") = String.Empty
    item.Attributes("TableID") = String.Empty
    item.Text = "Export to CSV"
    item.Value = "csv"
    menu.Items.Add(item)
 
End Sub

The save Layout works fine (calls GridSettingsPersister)

Any help would be much appreciated.

2 Answers, 1 is accepted

Sort by
0
Accepted
Daniel
Telerik team
answered on 22 Sep 2010, 12:44 PM
Hello Russell,

I do not recommend that you use the RegisterPostBackControl approach. Instead, my suggestion is to cancel the ajax request on the client as demonstrated below:

protected void Page_Load(object sender, EventArgs e)
{
    RadGrid1.HeaderContextMenu.PreRender += new EventHandler(HeaderContextMenu_PreRender);
    RadGrid1.HeaderContextMenu.OnClientItemClicking = "itemClicking";
}
 
void HeaderContextMenu_PreRender(object sender, EventArgs e)
{
    RadMenuItem item = new RadMenuItem("ExportToExcel");
    item.PostBack = false;
    (sender as RadContextMenu).Items.Add(item);
}

<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" ClientEvents-OnRequestStart="requestStart">

<script type="text/javascript">
    function requestStart(sender, args)
    {
        if (args.get_eventArgument().indexOf("ExportToExcel") != -1)
            args.set_enableAjax(false);
    }
 
    function itemClicking(sender, args)
    {
        if (args.get_item().get_text() == "ExportToExcel")
            $find("RadGrid1").get_masterTableView().fireCommand("ExportToExcel", "");
    }
</script>

Export from ajaxified grid

I hope this helps. Let me know if you need more information.

Best regards,
Daniel
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
mabs
Top achievements
Rank 1
answered on 22 Sep 2010, 05:13 PM
Thank you for the reply.

I couldn't use a RadAjaxPanel in my usercontrol, however your solution helped me to think of a different way,

function itemClicking(sender, args) {
    if (args.get_item().get_text() == "Export to CSV")
        __doPostBack('buildExport', 'excel');
}

I catch the click event clientside, then do a postback and call the function buildExport, in Page_Load I then catch the argument and target.

If Not (My.Request("__EVENTTARGET") Is Nothing OrElse My.Request("__EVENTTARGET") = "") Then
    Me.GetType.GetMethod(My.Request("__EVENTTARGET")).Invoke(Me, New Object() {Me.Request("__EVENTARGUMENT")})
End If

Thank you for your help.
Tags
Grid
Asked by
mabs
Top achievements
Rank 1
Answers by
Daniel
Telerik team
mabs
Top achievements
Rank 1
Share this question
or