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

Ajaxy RadGrid Exporting Fails after calling Rebind

9 Answers 80 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michael Malone
Top achievements
Rank 1
Michael Malone asked on 22 Oct 2012, 04:56 PM
I've been following the example here to allow a control in my CommandItemTemplate to export the grid to Excel.

http://www.telerik.com/community/code-library/aspnet-ajax/general/export-radgrid-content-to-excel-word-csv-pdf-with-ajax-enabled.aspx

When the page initially loads, the button works as expected and the performs the post-back which allows the data in the grid to be exported. However, if I were to press another button (not the export button) that DOES cause an Ajax response, then when I click the export button, the export button no longer causes a post-back. It's like it has reverted back to being ajaxified. 

This is in my RadGrid.ItemCreated event where I Register the Post Back on my export buttons

if (e.Item is GridCommandItem)
{
    ImageButton btnExport = (e.Item as GridCommandItem).FindControl("btnExport") as ImageButton;
    LinkButton btnExportLink = (e.Item as GridCommandItem).FindControl("btnExportLink") as LinkButton;
    RadScriptManager scriptManager = (RadScriptManager)Master.FindControl("RadScriptManager1");
    if (scriptManager != null)
    {
        scriptManager.RegisterPostBackControl(btnExport);
        scriptManager.RegisterPostBackControl(btnExportLink);
                        
    }
}

If I click a different button that causes an ajax request it breaks the exporting button. Code below that breaks the exporting.

void btnGo_Command(object sender, CommandEventArgs e)
{
    WeekNumber = Convert.ToInt32(ddlWeek.SelectedValue);
    Year = Convert.ToInt32(ddlYear.SelectedValue);
             
    SetWeekSelectWarningMessage(WeekNumber);
    SetMissingTotalsCount();
    SetPendingTotalsCount();
    grdTimecards.Rebind();
}

9 Answers, 1 is accepted

Sort by
0
Casey
Top achievements
Rank 1
answered on 22 Oct 2012, 07:28 PM
Hi Michael,

You could use the Client event for OnRequestStart  of your RadAjaxManager to disable Ajax if the event is triggered from the Export To Excel Button.

I hope this helps!
Casey

<script type="text/javascript">
 
        function onRequestStart(ajaxManager, args) {
            if (args.get_eventTarget().indexOf("ExportToExcelButton") >= 0) {
                args.set_enableAjax(false);
            }
        }
</script>
0
Michael Malone
Top achievements
Rank 1
answered on 25 Oct 2012, 02:32 PM
my RadAjaxManager is in the MasterPage and not on the actual aspx page. Will this still work? 
0
Casey
Top achievements
Rank 1
answered on 25 Oct 2012, 02:55 PM
I don't currently have a project setup that way, so I'm not 100% certain the args.get_eventTarget() would still be "ExportToExcelButton".

You could try adding it on your Master Page, and then modifying the javascript by adding "debugger;" to the function. This will allow you to set a breakpoint there, and then you should be able to check what the args.get_eventTarget() returns in the immediate window of Visual Studio.

I hope this helps!
Casey

0
Daniel
Telerik team
answered on 29 Oct 2012, 10:17 AM
Hi guys,

In a master/detail page scenario, the best approach would be to use RadAjaxManager and RadAjaxManagerProxy controls. The first control should be placed (just as in Michael's case) on the master page. This way you could use a single JS code block (for all pages) for disabling ajax when exporting.

Master page:
<script type="text/javascript">
    function onRequestStart(sender, args)
    {
        if (args.get_eventTarget().indexOf("ExportTo") >= 0)
        {
            args.set_enableAjax(false);
        }
    }
</script>

With this setup, you will be able cover all pages and also all built-in buttons (ExportToExcelButton, ExportToWordButton and the like).
If you need to use an external button you can name it using the same pattern - for example ExportToExcelBtn1.

Best regards,
Daniel
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Michael Malone
Top achievements
Rank 1
answered on 29 Oct 2012, 05:05 PM
If I put this javascript into my masterpage, can i remove the code-block that calls RegisterPostBackControl()?
0
VeriFone
Top achievements
Rank 1
answered on 30 Oct 2012, 04:30 PM
I have something similar. This code is placed in the Page.Load event of the child page

//This disables AJAX for the export button and registers the control for a full post back post.
// This is required for the grid export functionality to work correctly.
 
            Button btn = (RadGrid1.AllChildControls<Button>().First(ct => (ct.ID == "btnExport")));
            btn.Attributes.Add("onclick", string.Format("realPostBack(\"{0}\", \"\"); return false;", btn.UniqueID));

The AllChildControls function is a helper extension class as

public static IEnumerable<dynamic> AllChildControls<T>(this Control instance)
       {
 
           var controls = instance.Controls.Cast<Control>();
           return controls.SelectMany(AllChildControls<T>).Concat(controls).Where(c => c.GetType() == typeof(T));
       }

and the javascript registered in the materpage is via a web resource but the function looks like

function realPostBack(eventTarget, eventArgument) {
    __doPostBack(eventTarget, eventArgument);
}

I Hope this helps
0
Kostadin
Telerik team
answered on 01 Nov 2012, 03:13 PM
Hi Mark,

You can remove RegisterPostBackControl() method, as like my colleague's solution presents all the buttons which contains ExportTo in their IDs will cancel the ajax request.

I hope this helps.

All the best,
Kostadin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
VeriFone
Top achievements
Rank 1
answered on 01 Nov 2012, 03:22 PM
Hi Kostadin,

I don't use the RegisterPostBackControl() method. I was merely offering up an alternative solution.

Cheers
0
Kostadin
Telerik team
answered on 06 Nov 2012, 11:37 AM
Hello Mark,

This suggestion was for Michael but I have mistaken the name.

Greetings,
Kostadin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Michael Malone
Top achievements
Rank 1
Answers by
Casey
Top achievements
Rank 1
Michael Malone
Top achievements
Rank 1
Daniel
Telerik team
VeriFone
Top achievements
Rank 1
Kostadin
Telerik team
Share this question
or